1 /* --------------------------------------------------------------------  */
2 /*                          CALCULIX                                     */
3 /*                   - GRAPHICAL INTERFACE -                             */
4 /*                                                                       */
5 /*     A 3-dimensional pre- and post-processor for finite elements       */
6 /*              Copyright (C) 1996 Klaus Wittig                          */
7 /*                                                                       */
8 /*     This program is free software; you can redistribute it and/or     */
9 /*     modify it under the terms of the GNU General Public License as    */
10 /*     published by the Free Software Foundation; version 2 of           */
11 /*     the License.                                                      */
12 /*                                                                       */
13 /*     This program is distributed in the hope that it will be useful,   */
14 /*     but WITHOUT ANY WARRANTY; without even the implied warranty of    */
15 /*     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the      */
16 /*     GNU General Public License for more details.                      */
17 /*                                                                       */
18 /*     You should have received a copy of the GNU General Public License */
19 /*     along with this program; if not, write to the Free Software       */
20 /*     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.         */
21 /* --------------------------------------------------------------------  */
22 
23 
24 
25 #include <cgx.h>
26 
27 
28 #define     TEST            0   /* debugging */
29 #define     TEST1           0   /* debugging, substitute nurbs is kept */
30 
31 extern Display       *dpy;
32 extern int           dpycells;
33 extern Colormap      cmap;
34 extern XColor        *xcolor;
35 extern unsigned long *pixels_return;
36 extern unsigned int  npixels;
37 
38 extern int   w0, w1;                                      /* Fenster identifier  */
39 extern int   activWindow;                                 /* das aktuelle Fenster */
40 extern char  inpformat;
41 extern int   width_w1, height_w1;
42 extern double   aspectRatio_w1;                            /* width_w1/height_w1 */
43 extern double trackbsize;                                  /* TRACKBALLSIZE */
44 extern double curquat[4];                                  /* Matrix aus Trackball */
45 extern double lastquat[4];                                 /* letzte Matrix aus Trackball*/
46 
47 extern int   col_maxc,col_minc;
48 extern Entitycol *entitycol;
49 extern double     gtol;
50 extern int   steps;                                  /* Schrittweite der Farbscala */
51 extern int   offset, maxIndex;                       /* offset+steps-1 = maxIndex */
52 extern double dtx, dty, drx, dry, drz, ds;            /* Verschiebungen */
53 extern double centerPnt[3];                           /* Rotationszentrum */
54 extern double centerNode;
55 extern double dx ,dy;                                 /* Mauskoordinaten im bereich +-1*/
56 extern int   xpixels ,ypixels;                       /* Mauskoordinaten in pixel, links unten 0,0 */
57 extern GLint   gl_max_eval_order;                         /* max order of NURBS */
58 extern GLdouble R[4][4];                                   /* Rotationsmatrix */
59 extern GLdouble Rmem[4][4];
60 extern double v[4];                                        /* drehkorrekturen fuer centerPkt */
61 extern char  zoomFlag;                     /* (1) zoomModus */
62 extern char  centerFlag;                   /* (1) search centerPnt */
63 extern char  printFlag;                    /* printf 1:on 0:off */
64 extern char  flipColorFlag;                 /* 0: high values use red, low use blue in scale; 1: flipped */
65 extern char  graphFlag;
66 extern int   cur_entity;                                       /* aktive entity (component), entity in menu= cur_entity+1*/
67 extern int       cur_lc;                                          /* aktive Dataset */
68 extern char  datin[MAX_LINE_LENGTH];                          /* Input-data-file */
69 extern int   ddiv;
70 
71 extern char  mode[2];                        /* pickmode */
72 extern char  pickfunc[MAX_LINE_LENGTH];                     /* pickfunc either "qenq" "qadd" "qrem"   */
73 
74 extern int  pick_zmin;                         /* kleinster z-wert der gepickten items */
75 extern char pickname[MAX_LINE_LENGTH];                     /* name of the picked item   */
76 extern char  setname[MAX_LINE_LENGTH];                     /* setname-buffer for pick() */
77 
78 extern int     num_etype[13];
79 
80 extern Scale     scale[1];
81 extern Elements  *e_enqire;     /* elem-array by elem-number instead of elem[index]... */
82 extern Summen    anz[1];
83 extern Edges     *edge;
84 extern Nodes     *node;
85 extern Datasets *lcase;
86 extern Faces     *face;
87 
88 extern Alias     *alias;
89 extern Sets      *set;
90 extern Psets     *pset;
91 extern Values    *value;
92 extern Points    *point;
93 extern Lines     *line;
94 extern Lcmb      *lcmb;
95 extern Gsur      *surf;
96 extern Gbod      *body;
97 extern Nurbl     *nurbl;
98 extern Nurbs     *nurbs;
99 extern Shapes    *shape;
100 extern SumGeo    anzGeo[1];
101 extern SumAsci   sumAsci[1];
102 
103 
104 /* the copied node-sets which have to be filled with values from new loaded Datasets */
105 extern CopiedNodeSets copiedNodeSets[1];
106 
107 /* additional entities */
108 extern char **valuestack;
109 extern int valuestack_ptr, valuestackFlag;
110 extern int       offset;
111 extern SpecialSet specialset[1];
112 extern int set_bsur, set_glur;
113 
114 extern char **parameter;
115 
116 extern sem_t   sem_g;
117 extern sem_t   sem_rep;
118 
119 
120 /*---------------------------------------------------------*/
121 /* Liste aller unterstuetzten Funktionen                   */
122 /*---------------------------------------------------------*/
help(void)123 void help( void )
124 {
125   // remember for doc:
126   // swep, move etc 'rad' [<p1> <p2> <value> <div>] missing in latex doc
127   //
128   printf ("\n---------------------------------------------------------\n");
129   printf ("\n             Quick help for the experienced user \n");
130   printf ("\n---------------------------------------------------------\n");
131   printf ("\n\nSpecial Keys: \n");
132   printf (" ARROW_UP:   previous command \n");
133   printf (" ARROW_DOWN: next command \n");
134   printf (" PAGE_UP:    entities of previous set (if the last command was plot or plus) or the previous Loadcase\n");
135   printf (" PAGE_DOWN:  entities of next set (if the last command was plot or plus) or the next Loadcase\n\n");
136   printf ("\nMasking of entity names (alias) with a leading character: \n");
137   printf (" '!':    uses the successive name as an alias and generates a new unused entity name\n");
138   printf (" '\%%':    uses the successive name as an alias and replaces it by the referenced entity name\n");
139   printf (" '?':    uses the successive name as an entity name and replaces it by the referenced alias (only 'prnt')\n");
140   printf ("\nKnown commands and syntax: \n");
141   printf (" '..':   Keyword (either uppercase or lowercase)\n");
142   printf (" <..>:   Parameter (case-sensitive)\n");
143   printf (" [..]:   combination of parameters or optional parameter \n");
144   printf (" (..):   Remark \n");
145   printf (" |   :   OR \n");
146   printf (" &   :   AND \n");
147   printf (" -   :   from-to \n");
148   printf (" *chars*: wildcards are permitted\n");
149   printf (" RETURN: return-key to press \n\n");
150   printf ("   'allow_sys' (only in .cgx)\n");
151   printf ("   'anim' 'start'|'tune' <value>|'steps' <value>|'time' <value>|'real' ['on'|'off']|'model' ['on'|'off']|'elem' ['on'|'off']\n");
152   printf ("   'area' <set> \n");
153   printf ("   'asgn' 'n'|'e'|'p'|'l'|'c'|'s'|'b'|'S'|'L'|'se'|'sh'|'alpha'|'beta'|'nadapt' <value>|'netgen'|'tetgen'|'thrds' <value>|['rbe' <value>|'mpc']|'max'|'maxr'|'maxc' [<col>]|'minc' [<col>]|['mem' 'free'|'keep']|'usr' <text>]|[graph on|off|<nr>]\n");
154   printf ("   'bia'  [<set> [<bias> [<factor>]]|['mult'|'div' <factor>]] | [<line> <bias>]\n");
155   printf ("   'body' <name|!> [<set>]|[<surf1> <surf2>]|[<surf1> <surf2> <surf3> <surf4> <surf5> [<surf6>] [<surf7>]]\n");
156   printf ("   'break'\n");
157   printf ("   'call' <parameters> \n");
158   printf ("   'cmap' 'classic'|'inferno'|'turbo'|'viridis' \n");
159   printf ("   'cntr' <pnt|nod|set>|[x y z] \n");
160   printf ("   'col' <name> <red> <green> <blue>\n");
161   printf ("   'comp' <set|*chars*> 'c'|'d'|'e'|'u'\n");
162   printf ("   'cont' (continue after stop)\n");
163   printf ("   'copy' <set> <new_set> ['scal' <fx> <fy> <fz> [<pnt>]]|['tra' <x> <y> <z>]|['rad' [<p1> <p2> <value>]|['x'|'y'|'z' <value>]|[<p1> 'x'|'y'|'z' <value>]|['p'<PNT> <value>]]|['rot' [<p1> <p2> <value>]|['x'|'y'|'z' <value>]|[<p1> 'x'|'y'|'z' <value>]]|['mir' [<p1> <p2>]|['x'|'y'|'z']|[<p1> 'x'|'y'|'z']]|['nor' <value> [<div>]] [append]\n");
164   printf ("   'corrad' <set>\n");
165   printf ("   'csysa' <sysNr> <set>\n");
166   printf ("   'cut' <nod> | [<pnt|nod> <pnt|nod> <pnt|nod>]\n");
167   printf ("   'del'  ['p'|'l'|'l0'|'s'|'b'|'t'|'S'|'L'|'se'|'sh' <entity>]|['se0']|['mesh']|['pic']\n");
168   printf ("   'dist' <set>|[<set> <set>|<shpe> []]|['tra' <x> <y> <z> <offset> [<tol>]]|['rad' [<p1> <p2> <offset>]|['x'|'y'|'z' <offset>] [<tol>]]|['rot' [<p1> <p2> <offset>]|['x'|'y'|'z' <offset> [<tol>]]]|['nor' <offset> [<tol>]]\n");
169   printf ("   'div'  []|[<default>]|[<line> <div>]|[<set> ['auto' <node-dist> <angle> <elem-ratio>]|['mult'|'div' <factor-div> [<factor-bias>]]]\n");
170   printf ("   'ds' [[<1.Dataset-Nr>|'l'|<negative-ds> [<2.Dataset-Nr>|'l'|<negative-ds> [<3.Dataset-Nr>|'l'|<negative-ds>]]] ['a[h]' [<entity-nr>]]|['e[h]' <entity-nr> (up to 4 for vector-plots)]|['o' <offset> [<entity-nr>]]|['p' <power>] [<entity-nr>]]|['s' <factor> [<entity-nr>]]]|['g' <name> [[<ncomps>|<0>] <value> <text> <type> <step> <analysisName>]]|['e' <name> <comp> <type> <row> <column>]|['f']|['r' <key> [<parm1>] [..<parm5>]\n");
171   printf ("   'elem' <nr|!> [set]|[<firstNode>-<lastNode> 'be2'|'be3'|'tr3'|'tr6'|'qu4'|'qu8'|'he8'|'he20']\n");
172   printf ("   'else'\n");
173   printf ("   'else if' <value> 'eq'|'ne'|'=='|'!='|'<'|'>' <value>\n");
174   printf ("   'elty' [] | [<set> 'be2'|'be2r'|'be2f'|'be2d'|'be3'|'be3r'|'be3f'|'tr3'|'tr3u'|'tr3e'|'tr3s'|'tr3c'|'tr6'|'tr6u'|'tr6e'|'tr6s'|'tr6c'|'qu4'|'qu4e'|'qu4s'|'qu4c'|'qu4r'|'qu4er'|'qu4sr'|'qu4cr'|'qu8'|'qu8e'|'qu8s'|'qu8c'|'qu8r'|'qu8er'|'qu8sr'|'qu8cr'|'he8'|'he8f'|'he8i'|'he8r'|'he20'|'he20r'|'te4'|'te4f'|'te10'|'te10m'|'te10t'|'pe6'|'pe6f'|'pe6i'|'pe15'|'pe15r' [<parameter>]]\n");
175   printf ("   'endif'\n");
176   printf ("   'endwhile'\n");
177   printf ("   'enq' <set> <set> ['set' name]|'rec' <value>|'_' <value>|'_' <value>|'_']|['cx'|'cy'|'cz' <value>|'_'  <value>|'_' <value>|'_'] <tolvalue> 'i'|'a'|'h'|'l' [value]\n");
178   printf ("   'eprop' <set>\n");
179   printf ("   'eqal' 'jbir'|'aspr'|'mca' <value>\n");
180   printf ("   'exit' \n");
181   printf ("   'fil' <line> <line> <radius>\n");
182   printf ("   'flip' [<set> <e>|<b>|<s>]|[<s> ['auto']]\n");
183   printf ("   'flpc' \n");
184   printf ("   'font' 'd'|'l' <value(1-6)> \n");
185   printf ("   'frame' [<setname>]\n");
186   printf ("   'gbod' <name|!> 'NORM'|'ADD' ['+|-' <surf>] ..  \n");
187   printf ("   'gonly' 'on'|'off' \n");
188   printf ("   'graph' [<amplitude|*chars*> 'amp' [<l>]]|[<material|*chars*> 'mat' [<l>]]|[<set>|'-p' ['length' ['+'|'-']]|['step'|'nr'|'freq'|'time'|'descr'|<parameter> [<dataset> <entity|parameter> [<first-Dataset-Nr> [<last-Dataset-Nr>]] ] ] ] \n");
189   printf ("   'grpa' <grpNr> <set>\n");
190   printf ("   'grps' \n");
191   printf ("   'gsur' <name|!> ['+|-' 'BLEND|<nurbs>']|['ADD'] ['+|-' <line|lcmb>] .. \n");
192   printf ("   'gtol' []|<auto>|<geometric-tol> \n");
193   printf ("   'help' \n");
194   printf ("   'hcpy' [['ps'|'xwd'|'gif'|'png'] ['name']]|[make [ls]]|[clean] (def:xwd)\n");
195   printf ("   'if' <value> 'eq'|'ne'|'=='|'!='|'<'|'>' <value>\n");
196   printf ("   'int' <line> <line>\n");
197   printf ("   'init' <internal parameters>\n");
198   printf ("   'lcmb' <name|!> ['+|-' <line> '+|-' <line> ..(up to 14 lines)]|['ADD' '+|-' <line> '+|-' <line>..(up to 14 lines)] \n");
199   printf ("   'length' <set>\n");
200   printf ("   'line' <name|!> <p1> <p2> <cp|seq> <div> [<bias>]\n");
201   printf ("   'lnor' <name|!> <p1> <p2> <p3> <length>\n");
202   printf ("   'map' <slave-set> <master-set> ['volu' 'ds'[<nr>] ]|['surf' 'ds'[<nr>] ]|[ 'x'|'y'|'z'|'rx'|'ry'|'rz' 'ds'[<nr>] ] \n");
203   printf ("   'mata' <matNr> <set>\n");
204   printf ("   'mats' \n");
205   printf ("   'max' <value> ['e'|'f'|'i'|'l'] ['l'|'u']\n");
206   printf ("   'maxr' <value> ['e'|'f'|'i'|'l'] ['l'|'u']\n");
207   printf ("   'maxc' <value> ['e'|'f'|'i'|'l'] ['l'|'u']\n");
208   printf ("   'menu' <name> <command>\n");
209   printf ("   'merg' 'n'|'e'|'p'|'l'|'c'|'s' <set> <gtol> 'nolock' \n");
210   printf ("   'mesh' <set> ['fast'] ['tet' <size>|'block'|'lonly'|'nolength'|'noangle'|'length'|'angle']\n");
211   printf ("   'mids <set> ['lin'|'gen'|'rem']\n");
212   printf ("   'min' <value> ['e'|'f'|'i'|'l'] ['l'|'u']\n");
213   printf ("   'minr' <value> ['e'|'f'|'i'|'l'] ['l'|'u']\n");
214   printf ("   'minc' <value> ['e'|'f'|'i'|'l'] ['l'|'u']\n");
215   printf ("   'minus' 'n'|'e'|'p'|'l'|'s'|'b'|'S'|'L'|'sh' <set> \n");
216   printf ("   'move' <set> ['equ' <trgt-set> [<tol>]]|['scal' <fx> [<fy> <fz>] [<pnt>]]|['tra' <dx> <dy> <dz>]|['rot' [<p1> <p2>|['x'|'y'|'z'] <alfa>]|['x'|'y'|'z' [<alfa>|[<alfa1> <ax1> <alfa2> <ax2>]]]]||['rad' [<p1> <p2>|['x'|'y'|'z'] <alfa>]|['x'|'y'|'z'|'p'<pnt> [<dr>|<dr1> <ax1> <dr2> <ax2>]]]['mir' <P1> <P2>|['x'|'y'|'z']] \n");
217   printf ("   'movi' [loops <nr>]|[delay <sec>]|[start]|[stop]|[frames ['auto']|[<nr> [<epilogFile>]]]|[make <nr> <nr> <prolog.gif>]|[clean] \n");
218   printf ("   'mm' <value> ['f'|'i'|'e'] ['l'|'u']\n");
219   printf ("   'msg' 'on|off' \n");
220   printf ("   'mshp' <name> 's'|'b' <element-type-nr> <element-attr-nr> <density>|<size> \n");
221   printf ("   'neigh' <set> <tol> ['abq'|'ans'|'nas'] ['con'|'nsc' ['tie']|[[<stiffness>] [<mue>]]]|['equ' [<dofs('t'|'1-6')> 'c'|'u']|['tie' ['yes']]\n");
222   printf ("   'node' <nr|!> [<x> <y> <z> ['0'|'1']]|['v' <value> [<value> .. ]]|['vs' <value>] \n");
223   printf ("   'norm' <set> \n");
224   printf ("   'nurl'  <name|!> ['DEFINE' ['COMPACT'] <pstart> <pend> <deg> <npnt> <nknt> <div>]|['CONTROL' <index> [<pnt>|<x y z>] <weight>]|['KNOT' <index> <value>]|['END']\n");
225   printf ("   'nurs' [<name|!> ['DEFINE' ['COMPACT'] <u.deg> <v.deg> <u.npnt> <v.npnt> <u.nknt> <v.nknt>]|['CONTROL' <u.indx> <v.indx> [<pnt>|<x y z>] <weight>]|['KNOT' <u.indx> <v.indx> <value>]|['END']] | [ <!> <setname(containing surfaces)>]\n");
226   printf ("   'ori' <set>\n");
227   printf ("   'plot' ['n'|'e'|'f'|'p'|'l'|'s'|'b'|'S'|'L'|'sh']&['a'|'b'|'c'|'d'|'n'|'p'|'q'|'t'|'v'] <set> ['w'|'k'|'n'|'r'|'g'|'b'|'y'|'m'|'t'|'c'|'o'] [<width>]\n");
228   printf ("   'plus' ['n'|'e'|'f'|'p'|'l'|'s'|'b'|'S'|'L'|'sh']&['a'|'b'|'c'|'d'|'n'|'p'|'q'|'t'|'v'] <set> ['w'|'k'|'n'|'r'|'g'|'b'|'y'|'m'|'t'|'c'|'o'] [<width>]\n");
229   printf ("   'pnt' <name|!> [<x> <y> <z>]|[<L1> <ratio> <times>]|[<P1> <P2> <ratio> <times>]|[<setname(containing nodes)>]\n");
230   printf ("   'prnt' ['in']|['st' ['size']]|['usr']|['par' [<parameter>]|['amp' [<amplitude>|<*chars*>]|['mat' [<material>|<*chars*>]|['se'|'sq'|'eq' [<set>|<*chars*>]|['n'|'e' [<set>|<*chars*> ['range']]|['n'|'e'|'f'|'p'|'l'|'s'|'b'|'v'|'S'|'L' <entity>]\n");
231   printf ("   'proj' <set> <set>|<shpe> ['tra' <x> <y> <z> <offset> [<tol>]]|['rad' [<p1> <p2> <offset>]|['x'|'y'|'z' <offset>] [<tol>]]|['rot' [<p1> <p2> <offset>]|['x'|'y'|'z' <offset> [<tol>]]]|['nor' <offset> [<tol>]]\n");
232   printf ("   'qadd' <set> ['t'<value>] RETURN 'w'|'a'|'i'|'r'|'n'|'e'|'f'|'p'|'l'|'s'|'b'|'L'|'S'|'h'|'m'|'t'|'q'|'u'\n");
233   printf ("   'qali' RETURN 'w'|'p'|'n'|'q' \n");
234   printf ("   'qbia' RETURN 'w'|'a'|'i'|'1'-'9'|'c'|' '\n");
235   printf ("   'qbod' <name>(optional) RETURN 'w'|'b'|'a'|'i'|'r'|'s'|'g'|'q'|'u'\n");
236   printf ("   'qcnt' RETURN 'w'|'n'|'p' \n");
237   printf ("   'qcut' RETURN 'w'|'n'|'p'|'q'|'v' \n");
238   printf ("   'qdel' RETURN 'w'|'p'|'l'|'s'|'b'|'L'|'S'|'h'|'t'|'q'\n");
239   printf ("   'qdis' RETURN 'w'|'c'|'f'|'g'|'n'|'m'|'p'|'q'|'s'\n");
240   printf ("   'qdiv' RETURN 'w'|'a'|'i'|'1'-'9'|' '\n");
241   printf ("   'qenq' RETURN 'w'|'a'|'i'|'r'|'n'|'e'|'f'|'p'|'l'|'s'|'b'|'L'|'S'|'h'|'m'|'t'|'q'\n");
242   printf ("   'qfil' <radius> RETURN 'w'|'l'|'q' \n");
243   printf ("   'qflp' RETURN 'w'|'e'|'s'|'a'|'i'|'q' \n");
244   printf ("   'qint' RETURN 'w'|'l'|'q' \n");
245   printf ("   'qlin' <name>(optional) RETURN 'w'|'b'|'c'|'e'|'g'|'l'|'m'|'p'|'q'|'s'|'t'|'u'|'x'\n");
246   printf ("   'qmov' <name> RETURN 'w'|'p'|'m'|'u'\n");
247   printf ("   'qmsh' RETURN 'f'|'v'|'n'|'l'|'c'|'s'|'m'|'d'|'q'|'x'|'1'-'9'|' '\n");
248   printf ("   'qnod' RETURN 'w'|'p'|'m'|'u'\n");
249   printf ("   'qnor' RETURN 'w'|'p'\n");
250   printf ("   'qpnt' <name>(optional)RETURN 'w'|'p'|'g'|'m'|'n'|'s'|'S'|'u'\n");
251   printf ("   'qrem' <set> RETURN 'w'|'a'|'i'|'r'|'n'|'e'|'f'|'p'|'l'|'s'|'b'|'L'|'S'|'h'|'q'|'u'\n");
252   printf ("   'qseq' [[<set>] RETURN 'p']|[ RETURN [<nr>|'l'|'g']]\n");
253   printf ("   'qshp' RETURN 'w'|'p'|'g'|'s'|'S'|'h'|'c'|'q'\n");
254   printf ("   'qspl' RETURN 'w'|'s'|'q'\n");
255   printf ("   'qsur' <name>(optional) RETURN 'w'|'a'|'b'|'l'|'h'|'c'|'i'|'r'|'1'-'9'|'g'|'q'|'u'|'s'|'S'\n");
256   printf ("   'qtxt' RETURN 'g'|'a'|'i'|'b'|'p'|'m'|'n'|'v'|'f'|'d'|'q'\n");
257   printf ("   'quit' \n");
258   printf ("   'read' [<textfile> 'stack']|[<command-file> ['add']]|[<ccx-file> 'inp' ['add'|'ext'|'nom']]|[<ng-file> 'ng' ['add'|'ext'|'nom'|'ndsb']]|[<result-file> ['add'|'ext'|'new'|'nom'|<setname>]]|[<edge-file> 'edg']|[<list-file> '-n'|'-e'[<column>]]|[<picture.xwd> [<zoom>]|[<x_zoom> <y_zoom>]\n");
259   printf ("   'rep'  \n");
260   printf ("   'rnam' <set> <set> \n");
261   printf ("   'rot'  ['n' <set>|'nodenr'] | [['u'|'d'|'r'|'l'|'c' <angle>]|['x'|'-x'|'y'|'-y'|'z'|'-z']]\n");
262   printf ("   'save' \n");
263   printf ("   'scal' ['s'|'v'|'d'] [<value>]\n");
264   printf ("   'send' 'init'|[<set> ['abq'|'adh'|'ans'|'ast'|'bp'|'dar'|'fbd'|'frd'|'gmp'|'lst'|'nas'|'ng'|'pat'|'seq'|'stl'|'tcg'] []|['c'|'f'|'e']|['bin']|['dbin']|['comp']|['quadlin']|['names']|['raw']|['nor']|[ ['spc'|'spcf'] [<1-6|t|p> [<value>|['f' <value>]]|['ds'<nr>['+'] e<nr>]]]|['nor' <fac1> [<fac2> <fac3>]]|['slide'|'slidef' 'c'|'s'|'rx'|'ry'|'rz'|'tx'|'ty'|'tz']|['sur' [+|-]]|['pres' [<value>]|['ds'<nr> 'e'<nr>] [+|-]]|['force' <f1> <f2> <f3> ]|['film' [[<nodenr>]|[<temp>]|[['ds'<nr>]|[sq<dsnr>-<dsnr>]] 'e'<nr>] [[<coeff>]|[['ds'<nr>|[sq<dsnr>-<dsnr>]] 'e'<nr>]] [+|-]]|['rad' [[<temp>]|[cr<temp>]|['ds'<nr> 'e'<nr>]] [[<emis>|['ds'<nr> 'e'<nr>]]] [+|-]]|['dflux'|'mflow' [[<load>]|['ds'<nr> 'e'<nr>]] [+|-]]|['cflux' <load>]|['mpc' [[<rotation>|'v'<node> <v1> <v2> <v3> ]|['n'<node>]]]|['ds' <nr> 'e'<nr>[','|'-'<nr>..]]|['tmf']|['sta' <refspeed>]|['crp' <timefact> <refspeed> <writefreq>]]\n");
265   printf ("  OR 'send' <dep-set> <indep-set> 'nas'|'abq'|'ans'|'ids' ['cycmpc'|'cycmpcf' 'px'|'py'|'pz'|'tx'|'ty'|'tz'|'rx'|'ry'|'rz'|'cx'|'cy'|'cz'<segments> 'c'|'u'<NR>]|['areampc' [<1-6|t|p>|<1|2|3,px,py,pz,vx,vy,vz> 'c'|'u'<Nr>|'f'<value>|['slide']|['presfit' ['s'<value>]]]|['gap' <vx> <vy> <vz> <tol> ]\n");
266   printf ("  OR 'send' <set> 'foam' [<base-type> <set>]|['cyclic' <set> <set> 'rx'|'ry'|'rz'|'cx'|'cy'|'cz'|<vx,vy,vz>]\n");
267   printf ("  OR 'send' <set> 'isaac'|'duns' [<base-type> <set>]|['periodic' <set>]\n");
268   printf ("   'seqa' <set> [['afte'|'befo' <name>]|['end']] 'n'|'p' <name> <name>..] \n");
269   printf ("   'seqc' <set>\n");
270   printf ("   'seql' <set> <nr>\n");
271   printf ("   'seta' <set|'!'> ['v'|'n'|'e'|'p'|'l'|'ld'|'c'|'s'|'b'|'S'|'L'|'se'|'sh' <[\\]name|*chars*> ..]|['n'|'e' <name> '-' <name> <steps>]\n");
272   printf ("   'setc' <set> \n");
273   printf ("   'sete' <set> 'n'|'e'|'p'|'l'|'c'|'s'|'b'|'S'|'L'|'se'|'sh' 'max'|'min'|'strict'\n");
274   printf ("   'seti' <set> 'n'|'e'|'p'|'l'|'c'|'s'|'b'|'S'|'L'|'se'|'sh' <set> <set>.. \n");
275   printf ("   'seto' <set> \n");
276   printf ("   'setr' <set> 'v'|'n'|'e'|'p'|'l'|'ll'|'la'|'ls'|'ln'|'s'|'b'|'S'|'L'|'se'|'sh' <[\\]name|*chars*> .. \n");
277   printf ("   'shpe' <name|!> ['pln' <P1> <P2> <P3>] |['cyl' <P1> <P2> <R1>] |['con' <P1> <P2> <R1> <R2>] |['tor' <P1> <P2> <R1> <R2>] |['sph'] <P1> <R1>]\n");
278   printf ("   'split' [<set>(lines,surfs,tets) <set>(surfs,shape)] |[<line> <point>]\n");
279   printf ("   'stack' on|off|free (used by area,dist,ds,enq,gtol,length,prnt,valu,volu,..)\n");
280   printf ("   'steps' <value> \n");
281   printf ("   'stop' \n");
282   printf ("   'surf' <name|!> [<set>]|[<line|lcmb> <line|lcmb> <line|lcmb> [<line|lcmb>] [<line|lcmb>]] \n");
283   printf ("   'swep' <set> <new_set> ['scal' <fx> [<fy> <fz>] [<pnt>|<div>]]|['tra' <x> <y> <z> [<div>]]|['rad' [<p1> <p2> <value> [<div>]]|['x'|'y'|'z' <value> [<div>]]|[<p1> 'x'|'y'|'z' <value> [<div>]]|['p'<PNT> <dr> [<div>]]]|['rot' [<p1> <p2> <value> [<div>]]|['x'|'y'|'z' <value> [<div>]]|[<p1> 'x'|'y'|'z' <value> [<div>]]]|['mir' [<p1> <p2> [<div>]]|['x'|'y'|'z' [<div>]]|[<p1> 'x'|'y'|'z' [<div>]]]|['nor' <value> [<div>]] ['a'[rot:&|'n']]\n");
284   printf ("   'sys' <shell-command parameters> \n");
285   printf ("   'test' ['i' <set> <set>| ['o' <set>|'nodenr'] | ['d'|'v'|'n'|'e'|'p'|'l'|'c'|'s'|'b'|'S'|'L'|'se'|'sh' <name>]\n");
286   printf ("   'thrs' <value> 'h'|'l'|'o' ['t']\n");
287   printf ("   'tra' 'f'|'u'|'d'|'l'|'r' <relative-distance> \n");
288   printf ("   'trfm' 'rec'|'cyl' ['x'|'y'|'z'] [<first-Dataset-Nr> [<last-Dataset-Nr>]] (Datasets of a common type)\n");
289   printf ("   'txt' <set>|<node> ['n'|&'v'|&'e'|&'t'|&'f'|&'i'] [<x> <y>] [<\"text\">]\n");
290   printf ("   'typs' \n");
291   printf ("   'ucut' \n");
292   printf ("   'ulin' <string>\n");
293   printf ("   'val' (same as 'valu' but all un-masked <name> are substituted by its value)\n");
294   printf ("   'valu' <[!]name> [['push' [<splitkey>]]|['pop' [nr]]] | [<value> ['?' [<\"string\">]] |  ['&'|'*'|'/'|'+'|'-'|'abs'|'max'|'min'|'pow'|'sqr'|'sin'|'cos'|'tan'|'asin'|'acos'|'atan'|'int'|'float'|'exp' [name|<const> name|<const>]] ]\n");
295   printf ("   'view' ['cl' ['off']]|'fill'|'line'|['point' <value>]|['edge' ['off'|<value>]]|['elem' ['off']]|'surf'|'volu'|'front'|'back'|['vec' ['off']]|['disp' ['off']]|['bg' ['w'|'k']]|['sh' ['off']]|['ill' ['off']]|['rul' ['off'|<string>]]\n");
296   printf ("   'volu' <set>\n");
297   printf ("   'while' <value> 'eq'|'ne'||'=='|'!='|'<'|'>' <value>\n");
298   printf ("   'wpos' <xp> <yp>\n");
299   printf ("   'wsize' [['f']|[<xp> <yp>]]\n");
300   printf ("   'zap'  <set> \n");
301   printf ("   'zoom' [<scale>]|[<p1x> <p1y> <p2x> <p2y>] \n");
302 }
303 
304 
compareInt(int * a,int * b)305 int compareInt(int *a, int *b)
306 {
307   /* wird von qsort aufgerufen, vergleicht Integer-Felder */
308 
309   if ( a[0] < b[0] )
310     return (-1) ;
311   else if ( a[0] > b[0] )
312     return (1) ;
313   else
314     {
315     return (0) ;
316     }
317 }
318 
compareFloat(double * a,double * b)319 int compareFloat(double *a, double *b)
320 {
321   /* wird von qsort aufgerufen, vergleicht Float-Felder */
322 
323   if ( a[0] < b[0] )
324     return (-1) ;
325   else if ( a[0] > b[0] )
326     return (1) ;
327   else
328     {
329     return (0) ;
330     }
331 }
332 
compareRsort(Rsort * a,Rsort * b)333 int compareRsort(Rsort *a, Rsort *b)
334 {
335   /* wird von qsort aufgerufen, vergleicht Float-Felder */
336 
337   if ( a[0].r < b[0].r )
338     return (-1) ;
339   else if ( a[0].r > b[0].r )
340     return (1) ;
341   else
342     {
343     return (0) ;
344     }
345 }
346 
347 
pre_readfrdblock(CopiedNodeSets * copiedNodeSets,int lc,Summen * anz,Nodes * node,Datasets * lcase)348 int pre_readfrdblock( CopiedNodeSets *copiedNodeSets, int lc, Summen *anz,   Nodes     *node, Datasets *lcase )
349 {
350   if( readfrdblock(lc, anz, node, lcase )==-1)
351   {
352     //printf("ERROR in pre_readfrdblock: Could not read data for Dataset:%d\n", lc+1);
353     return(-1);
354   }
355   descalNodes ( anz->n, node, scale );
356   copyDatasetToNodes(anz, node, lcase, lc, copiedNodeSets[0]);
357   scalNodes ( anz->n, node, scale );
358   return(1);
359 }
360 
361 
362 
resetScaleValues(Scale * scale)363 void resetScaleValues( Scale *scale)
364 {
365   scale->x=scale->y=scale->z=0.; scale->w=1.;
366   scale->xmax=1.; scale->xmin=-1.;
367   scale->ymax=1.; scale->ymin=-1.;
368   scale->zmax=1.; scale->zmin=-1.;
369 }
370 
371 
372 
getScaleValues(int setNr,Sets * set,Points * point,Nodes * node,Scale * scale)373 void getScaleValues( int setNr, Sets *set, Points *point, Nodes *node, Scale *scale)
374 {
375   int  i,j;
376   /* -----------  MAX und MIN Werte feststellen -----------------  */
377 
378   scale->xmax=-MAX_FLOAT; scale->xmin=MAX_FLOAT;
379   scale->ymax=-MAX_FLOAT; scale->ymin=MAX_FLOAT;
380   scale->zmax=-MAX_FLOAT; scale->zmin=MAX_FLOAT;
381 
382   for (j=0; j<set[setNr].anz_n; j++ )
383   {
384     i=set[setNr].node[j];
385     if(node[i].pflag==0)
386     {
387     if (node[i].nx > scale->xmax) scale->xmax=node[i].nx;
388     if (node[i].nx < scale->xmin) scale->xmin=node[i].nx;
389     if (node[i].ny > scale->ymax) scale->ymax=node[i].ny;
390     if (node[i].ny < scale->ymin) scale->ymin=node[i].ny;
391     if (node[i].nz > scale->zmax) scale->zmax=node[i].nz;
392     if (node[i].nz < scale->zmin) scale->zmin=node[i].nz;
393     }
394   }
395   for (j=0; j<set[setNr].anz_p; j++ )
396   {
397     i=set[setNr].pnt[j];
398     if(point[i].name!=NULL)
399     {
400     if (point[i].px > scale->xmax) scale->xmax=point[i].px;
401     if (point[i].px < scale->xmin) scale->xmin=point[i].px;
402     if (point[i].py > scale->ymax) scale->ymax=point[i].py;
403     if (point[i].py < scale->ymin) scale->ymin=point[i].py;
404     if (point[i].pz > scale->zmax) scale->zmax=point[i].pz;
405     if (point[i].pz < scale->zmin) scale->zmin=point[i].pz;
406     }
407   }
408 
409   /* ------------  DATEN scalieren -------------------  */
410 
411   scale->x=(scale->xmax+scale->xmin)/2.;
412   scale->y=(scale->ymax+scale->ymin)/2.;
413   scale->z=(scale->zmax+scale->zmin)/2.;
414   scale->xmax=scale->xmax-scale->x;
415   scale->ymax=scale->ymax-scale->y;
416   scale->zmax=scale->zmax-scale->z;
417   scale->xmin=scale->xmin-scale->x;
418   scale->ymin=scale->ymin-scale->y;
419   scale->zmin=scale->zmin-scale->z;
420   if (scale->xmax < (-scale->xmin)) scale->xmax=(-scale->xmin);
421   if (scale->ymax < (-scale->ymin)) scale->ymax=(-scale->ymin);
422   if (scale->zmax < (-scale->zmin)) scale->zmax=(-scale->zmin);
423   scale->w=scale->xmax;
424   if (scale->w < scale->ymax){ scale->w=scale->ymax;}
425   if (scale->w < scale->zmax){ scale->w=scale->zmax;}
426 
427   scale->w/=0.4; /* nochmal scaliert */
428   if (scale->w<=0.) scale->w=1.;
429   //printf("scale: %f %f %f %f\n", scale->x, scale->y, scale->z, scale->w);
430 }
431 
432 
defineColTextur_load(float alpha)433 void defineColTextur_load(float alpha)
434 {
435   int   i, n, buf;
436   float r, g, b;
437   extern GLfloat   *contur_tex;
438   GLuint tex_id;
439 
440   /* define colormap values in both colormaps */
441   if( (contur_tex= (GLfloat *)realloc( (GLfloat *)contur_tex, ((TEX_PIXELS+1)*4)*sizeof(GLfloat ) ))==NULL )
442     printf("ERROR: realloc failed: defineColTextur_load \n\n" );
443 
444   if(steps>TEX_PIXELS) steps=TEX_PIXELS;
445 
446   n=0;
447   for (i=0; i<steps; i++)
448   {
449     buf=i;
450     if(!flipColorFlag)
451     {
452       if(scale->smaxr==2) buf++;
453       define_rgb( (float)buf/(steps-1), &r,&g,&b);
454     }
455     else
456     {
457       if(scale->sminr==2) buf--;
458       define_rgb( (steps-1-(float)buf)/(steps-1.), &r,&g,&b);
459     }
460 
461     if(i>=steps-1 && scale->smaxr==2)
462     {
463     contur_tex[n]  =entitycol[col_maxc].r;
464     contur_tex[n+1]=entitycol[col_maxc].g;
465     contur_tex[n+2]=entitycol[col_maxc].b;
466     contur_tex[n+3]=alpha;
467     }
468     else if(i<=0 && scale->sminr==2)
469     {
470     contur_tex[n]  =entitycol[col_minc].r;
471     contur_tex[n+1]=entitycol[col_minc].g;
472     contur_tex[n+2]=entitycol[col_minc].b;
473     contur_tex[n+3]=alpha;
474     }
475     else
476     {
477     contur_tex[n]  =r;
478     contur_tex[n+1]=g;
479     contur_tex[n+2]=b;
480     contur_tex[n+3]=alpha;
481     }
482     // value dependent blending
483     //contur_tex[n+3]=alpha+((1-alpha)*(double)i/(double)steps);
484     n+=4;
485   }
486   for (; i<TEX_PIXELS; i++)
487   {
488     if(i>=steps-1 && scale->smaxr==2)
489     {
490     contur_tex[n]  =entitycol[col_maxc].r;
491     contur_tex[n+1]=entitycol[col_maxc].g;
492     contur_tex[n+2]=entitycol[col_maxc].b;
493     contur_tex[n+3]=alpha;
494     }
495     else if(i<=0 && scale->sminr==2)
496     {
497     contur_tex[n]  =entitycol[col_minc].r;
498     contur_tex[n+1]=entitycol[col_minc].g;
499     contur_tex[n+2]=entitycol[col_minc].b;
500     contur_tex[n+3]=alpha;
501     }
502     else
503     {
504     contur_tex[n]  =r;
505     contur_tex[n+1]=g;
506     contur_tex[n+2]=b;
507     contur_tex[n+3]=alpha;
508     n+=4;
509     }
510   }
511 
512   /*
513   n=0;
514   for (i=0; i<TEX_PIXELS; i++)
515   {    printf("%d %d %lf %lf %lf %lf\n",steps, i, contur_tex[n], contur_tex[n+1], contur_tex[n+2], contur_tex[n+3]); n+=4; }
516   */
517   glGenTextures(1, &tex_id);
518   glBindTexture(GL_TEXTURE_1D, tex_id);
519   glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
520   glTexParameterf(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
521   glTexParameterf(GL_TEXTURE_1D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
522   glTexParameterf(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
523   glTexParameterf(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
524   glTexImage1D(GL_TEXTURE_1D, 0, 4, TEX_PIXELS, 0, GL_RGBA, GL_FLOAT, contur_tex);
525 }
526 
527 
528 
rot_u(double a)529 void rot_u(double a)
530 {
531   int i,n;
532   double b;
533 
534     b=a/90.; i=b;
535     a=a*PI/180.;
536     if(i<0) for(n=0; n<-i; n++)
537     {
538       trackball( 1, trackbsize, curquat, 0.0, 0.0, 0.0, trackbsize );
539       add_quats(curquat, lastquat, lastquat);
540       build_rotmatrix( R, lastquat );
541     }
542     if(i>0) for(n=0; n<i; n++)
543     {
544       trackball( 1, trackbsize, curquat, 0.0, 0.0, 0.0, -trackbsize );
545       add_quats(curquat, lastquat, lastquat);
546       build_rotmatrix( R, lastquat );
547     }
548     a=(b-i)*PI/2.;
549     trackball( 1, trackbsize, curquat, 0.0, 0.0, 0.0, -sin(a)*trackbsize );
550     add_quats(curquat, lastquat, lastquat);
551     build_rotmatrix( R, lastquat );
552 }
rot_r(double a)553 void rot_r(double a)
554 {
555   int i,n;
556   double b;
557 
558     b=-a/90.; i=b;
559     a=-a*PI/180.;
560     if(i<0) for(n=0; n<-i; n++)
561     {
562       trackball( 1, trackbsize, curquat, 0.0, 0.0, -trackbsize, 0.0 );
563       add_quats(curquat, lastquat, lastquat);
564       build_rotmatrix( R, lastquat );
565     }
566     if(i>0) for(n=0; n<i; n++)
567     {
568       trackball( 1, trackbsize, curquat, 0.0, 0.0, trackbsize, 0.0 );
569       add_quats(curquat, lastquat, lastquat);
570       build_rotmatrix( R, lastquat );
571     }
572     a=(b-i)*PI/2.;
573     trackball( 1, trackbsize, curquat, 0.0, 0.0, sin(a)*trackbsize, 0.0 );
574     add_quats(curquat, lastquat, lastquat);
575     build_rotmatrix( R, lastquat );
576 }
rot_c(double a)577 void rot_c(double a)
578 {
579   int i,n;
580   double b;
581 
582     b=a/90.; i=b;
583     a=a*PI/180.;
584     if(i<0) for(n=0; n<-i; n++)
585     {
586       trackball( 1, trackbsize, curquat, trackbsize, 0.0, 0.0, -trackbsize );
587       add_quats(curquat, lastquat, lastquat);
588       build_rotmatrix( R, lastquat );
589     }
590     if(i>0) for(n=0; n<i; n++)
591     {
592       trackball( 1, trackbsize, curquat, trackbsize, 0.0, 0.0, trackbsize );
593       add_quats(curquat, lastquat, lastquat);
594       build_rotmatrix( R, lastquat );
595     }
596     a=(b-i)*PI/2.;
597     trackball( 1, trackbsize, curquat, trackbsize, 0.0, cos(a)*trackbsize, sin(a)*trackbsize );
598     add_quats(curquat, lastquat, lastquat);
599     build_rotmatrix( R, lastquat );
600 }
601 
rot_x(double a)602 void rot_x(double a)
603 {
604     /* Trackballfunktion inizialisieren, Blickrichtung +x */
605     a*=90.*PI/180.;
606     trackball( 1, trackbsize, lastquat, 0.0, 0.0, sin(a)*trackbsize, 0.0 );
607     R[0][0]=cos(a);  R[1][0]=0.;  R[2][0]=sin(a);  R[3][0]=0.;
608     R[0][1]=0.;  R[1][1]=1.;  R[2][1]=0.;  R[3][1]=0.;
609     R[0][2]=-sin(a); R[1][2]=0.;  R[2][2]=cos(a);  R[3][2]=0.;
610     R[0][3]=0.;  R[1][3]=0.;  R[2][3]=0.;  R[3][3]=1.;
611 }
612 
rot_y(double a)613 void rot_y(double a)
614 {
615     /* Trackballfunktion inizialisieren, Blickrichtung +y (rot_x)*/
616     a*=-90.*PI/180.;
617     trackball( 1, trackbsize, lastquat, 0.0, 0.0, 0.0, -sin(a)*trackbsize );
618     R[0][0]=1.;  R[1][0]=0.;  R[2][0]=0.;  R[3][0]=0.;
619     R[0][1]=0.;  R[1][1]=cos(a);  R[2][1]=-sin(a);  R[3][1]=0.;
620     R[0][2]=0.;  R[1][2]=sin(a);  R[2][2]=cos(a);  R[3][2]=0.;
621     R[0][3]=0.;  R[1][3]=0.;  R[2][3]=0.;  R[3][3]=1.;
622 }
623 
rot_z(double b)624 void rot_z(double b)
625 {
626   double a;
627     /* Trackballfunktion inizialisieren, Blickrichtung +z (rot_z)*/
628     a=0.;
629     trackball( 1, trackbsize, lastquat, trackbsize, 0.0, cos(a)*trackbsize, sin(a)*trackbsize );
630     R[0][0]=cos(a);  R[1][0]=-sin(a);  R[2][0]=0.;  R[3][0]=0.;
631     R[0][1]=sin(a);  R[1][1]=cos(a);   R[2][1]=0.;  R[3][1]=0.;
632     R[0][2]=0.;  R[1][2]=0.;  R[2][2]=1.;  R[3][2]=0.;
633     R[0][3]=0.;  R[1][3]=0.;  R[2][3]=0.;  R[3][3]=1.;
634     if(b==1) rot_r(180.);
635 }
636 
rot_norm(int nr)637 int rot_norm(int nr)
638 {
639   int    setcopy;
640   static int *sum_n=NULL;
641   static Nodes *norm=NULL;
642 
643   static double p03[3], p03xz[3];
644   double  ay, axz;
645   double  p03_b, p03xz_b, scalar, sig;
646 
647   /* determine the normal based on all connected faces */
648   delSet("+norm");
649   setcopy=pre_seta( "+norm", "i", 0);
650   if (setcopy<0)
651   {
652     errMsg (" ERROR: set:+norm could not be created\n" );
653     return(1);
654   }
655   seta( setcopy, "n", nr );
656   completeSet("+norm", "up") ;
657   completeSet("+norm", "do") ;
658   getNodeNormalen(&sum_n, &norm, setcopy, anz, face);
659   delSet("+norm");
660 
661   p03[0]=norm[nr].nx;
662   p03[1]=norm[nr].ny;
663   p03[2]=norm[nr].nz;
664   if(v_betrag(p03)<0.5) return(2);
665 
666   center( node[nr].nx, node[nr].ny, node[nr].nz );
667 
668   /* drehe den Ort des Betrachters  */
669   ay=atan(p03[0]/p03[2]);
670 
671   p03xz[0]=p03[0];
672   p03xz[1]=0.;
673   p03xz[2]=p03[2];
674   scalar = v_sprod( p03xz, p03 );
675   p03xz_b= v_betrag( p03xz );
676   p03_b  = v_betrag( p03 );
677   axz =acos( scalar/p03xz_b/p03_b ) * 180./PI;
678 
679   /* vorzeichen von der y-Komponente der Normalen wird zum vorzeichen von axz */
680   scalar= sqrt(p03[1]*p03[1]);
681   sig = p03[1]/scalar;
682   axz = axz*sig;
683   ay=ay*180./PI;
684 
685   /* Richtungsumkehr wenn die z-Komponente der Normalen nach hinten zeigt (-) */
686   if (p03[2]<0)
687   {
688     rot_z(1.);
689   }
690   else rot_z(-1.);
691   if((ay>=-360.)&&(ay<=360.)) rot_r( ay );
692   if((axz>=-360.)&&(axz<=360.)) rot_u( axz );
693   return(0);
694 }
695 
696 
697 
transformResults(char * record)698 void transformResults( char *record )
699 {
700   int i;
701   char method[MAX_LINE_LENGTH], axis[MAX_LINE_LENGTH];
702   int lcmin=0, lcmax=0;
703   int lc, *dsNr=NULL, anz_lc=0;
704 
705   if(!anz->l) return;
706 
707   sscanf(record,"%s %s %d %d", method, axis, &lcmin, &lcmax);
708   lcmin--; lcmax--;
709   if(lcmin<0) { lcmin=lcmax=cur_lc; }
710   if(lcmax<0) lcmax=lcmin;
711   if(lcmin>anz->olc) lcmin=anz->olc;
712   if(lcmax>anz->olc) lcmax=anz->olc;
713 
714   /* get the list of datasets */
715   for(lc=lcmin; lc<=lcmax; lc++)
716   {
717     //if( compare( lcase[lc].name, dataset, strlen(lcase[lcmin].name)) == strlen(lcase[lcmin].name) )
718     if(compareStrings(lcase[lc].name, lcase[lcmin].name)>0)
719     {
720       anz_lc++;
721       if((dsNr=(int *)realloc((int *)dsNr, (anz_lc+2) *sizeof(int)))==NULL )
722         printf("\n\n ERROR: malloc failed \n\n") ;
723       dsNr[anz_lc]=lc;
724     }
725   }
726   if(!anz_lc)
727   {
728     printf(" ERROR: found no matching Dataset for string:%s\n", lcase[lcmin].name);
729     return;
730   }
731   dsNr[0]=anz_lc;
732 
733   /* check if the data of the specified lcase (Dataset) are already available */
734   printf (" please wait, loading data\n");
735   for(i=1; i<=dsNr[0]; i++)
736   {
737     lc=dsNr[i];
738     if (!lcase[lc].loaded)
739     {
740       if( pre_readfrdblock(copiedNodeSets , lc, anz, node, lcase )==-1)
741       {
742         printf("ERROR in transformResults: Could not read data for Dataset:%d\n", lc+1);
743         return;
744       }
745       calcDatasets( lc, anz, node, lcase );
746       recompileEntitiesInMenu(lc);
747     }
748 
749     /* transform dataset */
750     printf("transform dataset nr:%d \n", lc+1);
751     transformDatasets( lc, anz, node, lcase, method, axis );
752     recompileEntitiesInMenu(lc);
753   }
754 }
755 
756 
757 
758 /* transformiert punkte-koordinaten und liefert die methode der transformation zurueck */
759 /*
760    fehler: -1
761    tra: 1
762    rad: 2
763    rot: 3
764    sca: 4
765    mir: 5
766    equ: 6
767    nor: 7
768 */
transform(char * record,int anz_n,Nodes * nslave)769 int transform( char *record, int anz_n, Nodes *nslave )
770 {
771   int  length, i,j;
772   char v[MAX_LINE_LENGTH], dat[4][MAX_LINE_LENGTH];
773   char type[MAX_LINE_LENGTH], pkt1[MAX_LINE_LENGTH], pkt2[MAX_LINE_LENGTH];
774   int   pnr1, pnr2, setNr;
775   double fi=0., dx=0., dy=0., dz=0., dr=0., x, y, z, r, dh, dq, l, h, q, h_offs, q_offs, tol;
776   double val[3]={0,0,0}, xdr1, xdr2, dr1, dr2;
777   double s[4]={0,0,0,0}, p1[3], p2[3], p1p2[3];
778   double ph[3], p1ph[3];
779   double el[3], eh[3], eq[3];   /* xyz koordinaten der lhq vektoren */
780   double ex[3]={1.,0.,0.}, ey[3]={0.,1.,0.}, ez[3]={0.,0.,1.};   /* lhq koordinaten der xyz vektoren */
781   double sprod;
782   double ep1p2[3],p1n0[3],p1n1[3],n0[3],n1[3],n1n0[3],en1n0[3],n1n2[3],n2[3];
783   static Nodes *norm=NULL;
784   static int *sum_n=NULL;
785 
786   length = sword( record, type );
787 
788   if(( compare(type,"tra",3) == 3 )||( compare(type,"TRA",3) == 3 ))
789   {
790     sscanf( record, "%*s %lf %lf %lf", &dx, &dy, &dz );
791     for (i=0; i<anz_n; i++)
792     {
793       nslave[i].nx+= dx;
794       nslave[i].ny+= dy;
795       nslave[i].nz+= dz;
796     }
797     return(1);
798   }
799   else if(( compare(type,"nor",3) == 3 )||( compare(type,"NOR",3) == 3 ))
800   {
801     // works only for nodes
802     sscanf( record, "%*s %lf", &dr );
803 
804     /* determine the normal based on all connected faces, code from pre_norm()  */
805     delSet("+norm");
806     setNr=pre_seta( "+norm", "i", 0);
807     if (setNr<0)
808     {
809       errMsg (" ERROR: set:+norm could not be created\n" );
810       return(-1);
811     }
812     for (i=0; i<anz_n; i++)
813     {
814       seta( setNr, "n", nslave[i].indx  );
815     }
816     completeSet("+norm", "do") ;
817     getNodeNormalen(&sum_n, &norm, setNr, anz, face);
818     delSet("+norm");
819     for (i=0; i<anz_n; i++)
820     {
821       nslave[i].nx+= norm[nslave[i].indx].nx*dr;
822       nslave[i].ny+= norm[nslave[i].indx].ny*dr;
823       nslave[i].nz+= norm[nslave[i].indx].nz*dr;
824     }
825     return(7);
826   }
827   else if(( compare(type,"equ",3) == 3 )||( compare(type,"EQU",3) == 3 ))
828   {
829     tol=gtol;
830     sscanf( record, "%*s %s %lf", dat[0], &tol );
831     setNr=getSetNr( dat[0] );
832     if (setNr<0)
833     {
834       errMsg(" ERROR: Set (%s) is undefined\n", dat[0] );
835       return(-1);
836     }
837     if ((set[setNr].anz_p<1)&&(set[setNr].anz_n<1))
838     {
839       errMsg(" ERROR: Set (%s) holds no target-nodes or points\n", dat[0] );
840       return(-1);
841     }
842 
843     for(j=0; j<set[setNr].anz_p; j++)
844     {
845       pnr1=set[setNr].pnt[j];
846       for (i=0; i<anz_n; i++)
847       {
848         dx= point[pnr1].px*scale->w+scale->x-nslave[i].nx;
849         dy= point[pnr1].py*scale->w+scale->y-nslave[i].ny;
850         dz= point[pnr1].pz*scale->w+scale->z-nslave[i].nz;
851 	//printf("dr=%f tol:%f\n", sqrt(dx*dx+dy*dy+dz*dz), tol);
852         if(sqrt(dx*dx+dy*dy+dz*dz) <= tol)
853 	{
854           nslave[i].nx=point[pnr1].px*scale->w+scale->x;
855           nslave[i].ny=point[pnr1].py*scale->w+scale->y;
856           nslave[i].nz=point[pnr1].pz*scale->w+scale->z;
857 	}
858       }
859     }
860 
861     for(j=0; j<set[setNr].anz_n; j++)
862     {
863       pnr1=set[setNr].node[j];
864       for (i=0; i<anz_n; i++)
865       {
866         dx= node[pnr1].nx*scale->w+scale->x-nslave[i].nx;
867         dy= node[pnr1].ny*scale->w+scale->y-nslave[i].ny;
868         dz= node[pnr1].nz*scale->w+scale->z-nslave[i].nz;
869 	//printf("dr=%f tol:%f\n", sqrt(dx*dx+dy*dy+dz*dz), tol);
870         if(sqrt(dx*dx+dy*dy+dz*dz) <= tol)
871 	{
872           nslave[i].nx=node[pnr1].nx*scale->w+scale->x;
873           nslave[i].ny=node[pnr1].ny*scale->w+scale->y;
874           nslave[i].nz=node[pnr1].nz*scale->w+scale->z;
875 	}
876       }
877     }
878     return(6);
879   }
880   else if(( compare(type,"sca",3) == 3 )||( compare(type,"SCA",3) == 3 ))
881   {
882     length=sscanf( record, "%*s %s %s %s %s", dat[0], dat[1], dat[2], dat[3] );
883     for(i=0; i<length; i++) { s[i]=atof(dat[i]);  }
884 
885     pnr1=getPntNr(dat[length-1]);
886     if (pnr1<0)
887     {
888       /* check if we have a valid number */
889       if (s[length-1]==0.)
890       {
891         errMsg(" Argument:%s is not valid\n", dat[length-1] );
892         return(-1);
893       }
894     }
895     else length--;
896 
897     if (length==0) s[1]=s[2]=s[0]=1.;
898     if (length==1) s[1]=s[2]=s[0];
899     if (length==2) s[2]=1.;
900     if (pnr1>-1)
901     {
902       ph[0]=(point[pnr1].px*scale->w+scale->x);
903       ph[1]=(point[pnr1].py*scale->w+scale->y);
904       ph[2]=(point[pnr1].pz*scale->w+scale->z);
905     }
906     else ph[0]=ph[1]=ph[2]=0.;
907 
908     for (i=0; i<anz_n; i++)
909     {
910       nslave[i].nx= ph[0] - s[0]*(ph[0]-nslave[i].nx);
911       nslave[i].ny= ph[1] - s[1]*(ph[1]-nslave[i].ny);
912       nslave[i].nz= ph[2] - s[2]*(ph[2]-nslave[i].nz);
913     }
914     return(4);
915   }
916   else if(( compare(type,"rad",3) == 3 )||( compare(type,"RAD",3) == 3 ))
917   {
918     length=sscanf( record, "%*s%s%s%lf%lf%lf", pkt1, pkt2, &val[0], &val[1], &val[2] );
919 
920     if(checkIfNumber(pkt2))
921     {
922       strcpy(v,pkt1);
923       if (v[0]=='p')
924       {
925         strcpy(pkt1, &v[1]);
926         pnr1=getPntNr( pkt1 );
927         if (pnr1==-1)
928         {
929           errMsg(" ERROR: Point (%s) is undefined\n", pkt1 );
930           return(-1);
931         }
932         ph[0]=(point[pnr1].px*scale->w+scale->x);
933         ph[1]=(point[pnr1].py*scale->w+scale->y);
934         ph[2]=(point[pnr1].pz*scale->w+scale->z);
935       }
936 
937       for (i=0; i<anz_n; i++)
938       {
939         x=nslave[i].nx;
940         y=nslave[i].ny;
941         z=nslave[i].nz;
942 
943         if(length==5)
944 	{
945           dr1=atof(pkt2);
946           xdr1=val[0];
947           dr2=val[1];
948           xdr2=val[2];
949           if (v[0]=='x') dr=(x-xdr1)/(xdr2-xdr1)*(dr2-dr1) + dr1;
950           if (v[0]=='y') dr=(y-xdr1)/(xdr2-xdr1)*(dr2-dr1) + dr1;
951           if (v[0]=='z') dr=(z-xdr1)/(xdr2-xdr1)*(dr2-dr1) + dr1;
952 	}
953 	else dr=atof(pkt2);
954 
955         if (v[0]=='x')
956         {
957           r=sqrt(y*y+z*z);
958           if (r>gtol)
959           {
960             y=y*(r+dr)/r;
961             z=z*(r+dr)/r;
962           }
963         }
964         else if (v[0]=='y')
965         {
966           r=sqrt(x*x+z*z);
967           if (r>gtol)
968           {
969             x=x*(r+dr)/r;
970             z=z*(r+dr)/r;
971           }
972         }
973         else if (v[0]=='z')
974         {
975           r=sqrt(y*y+x*x);
976           if (r>gtol)
977           {
978             y=y*(r+dr)/r;
979             x=x*(r+dr)/r;
980           }
981         }
982         else if (v[0]=='p')
983         {
984           dx=x-ph[0];
985           dy=y-ph[1];
986           dz=z-ph[2];
987           r=sqrt(dy*dy+dx*dx+dz*dz);
988           if (r>gtol)
989           {
990             x=ph[0]+dx*(r+dr)/r;
991             y=ph[1]+dy*(r+dr)/r;
992             z=ph[2]+dz*(r+dr)/r;
993           }
994         }
995         else
996         { errMsg(" rotational Axis not known:%1s \n", v); return(-1); }
997         nslave[i].nx= x;
998         nslave[i].ny= y;
999         nslave[i].nz= z;
1000       }
1001     }
1002     else
1003     {
1004       dr=val[0];
1005       pnr1=getPntNr( pkt1 );
1006       pnr2=getPntNr( pkt2 );
1007       if (pnr1==-1)
1008       {
1009         errMsg(" ERROR: Point1 (%s) is undefined\n", pkt1 );
1010         return(-1);
1011       }
1012       else
1013       {
1014         p1[0] = point[pnr1].px*scale->w+scale->x;
1015         p1[1] = point[pnr1].py*scale->w+scale->y;
1016         p1[2] = point[pnr1].pz*scale->w+scale->z;
1017       }
1018       if (pnr2==-1)
1019       {
1020         if(compareStrings(pkt2,"x")>0) v_add(p1, ex, p2);
1021         else if(compareStrings(pkt2,"y")>0) v_add(p1, ey, p2);
1022         else if(compareStrings(pkt2,"z")>0) v_add(p1, ez, p2);
1023         else { errMsg(" ERROR: Point2 (%s) is undefined\n", pkt2 );        return(-1); }
1024       }
1025       else
1026       {
1027         p2[0] = point[pnr2].px*scale->w+scale->x;
1028         p2[1] = point[pnr2].py*scale->w+scale->y;
1029         p2[2] = point[pnr2].pz*scale->w+scale->z;
1030       }
1031       v_result(p1,p2,p1p2);
1032 
1033       for (i=0; i<anz_n; i++)
1034       {
1035         n0[0]=nslave[i].nx;
1036         n0[1]=nslave[i].ny;
1037         n0[2]=nslave[i].nz;
1038         v_result(p1,n0,p1n0);
1039         if(v_betrag(p1n0)<1.e-20) continue;
1040 
1041         /* projection of nslave onto p1p2 with: unit-vector * scalar-product */
1042         v_norm(p1p2, ep1p2);
1043         sprod=v_sprod(ep1p2,p1n0);
1044         v_scal(&sprod,ep1p2, p1n1);
1045 
1046         /* projection point n1 */
1047         v_add(p1,p1n1,n1);
1048 
1049         /* unit vector to radial moved point n2 */
1050         v_result(n1,n0,n1n0);
1051         v_norm(n1n0, en1n0);
1052 
1053         /* distance from n1 to n2 */
1054         l=v_betrag(n1n0);
1055 
1056         /* do not move locations which are very close to the axis */
1057         if(l>gtol) l+=dr;
1058         //else printf(" i:%d to close to cl to be moved radially\n",i);
1059 
1060         /* vector from n1 to n2 */
1061         v_scal(&l,en1n0, n1n2);
1062 
1063         /* point n2 */
1064         v_add(n1,n1n2,n2);
1065 
1066         nslave[i].nx= n2[0];
1067         nslave[i].ny= n2[1];
1068         nslave[i].nz= n2[2];
1069       }
1070     }
1071     return(2);
1072   }
1073   else if(( compare(type,"rot",3) == 3 )||( compare(type,"ROT",3) == 3 ))
1074   {
1075     length=sscanf( record, "%*s%s%s%lf%lf%lf", pkt1, pkt2, &val[0], &val[1], &val[2] );
1076     i=sscanf( record, "%*s%s%s%lf", pkt1, pkt2, &fi );
1077 
1078     if(checkIfNumber(pkt2))
1079     {
1080       strcpy(v,pkt1);
1081 
1082       for (i=0; i<anz_n; i++)
1083       {
1084         x=nslave[i].nx;
1085         y=nslave[i].ny;
1086         z=nslave[i].nz;
1087 
1088         if(length==5)
1089 	{
1090           dr1=atof(pkt2);
1091           xdr1=val[0];
1092           dr2=val[1];
1093           xdr2=val[2];
1094           if (v[0]=='x') dr=(x-xdr1)/(xdr2-xdr1)*(dr2-dr1) + dr1;
1095           if (v[0]=='y') dr=(y-xdr1)/(xdr2-xdr1)*(dr2-dr1) + dr1;
1096           if (v[0]=='z') dr=(z-xdr1)/(xdr2-xdr1)*(dr2-dr1) + dr1;
1097 	  fi=dr*PI/180.;
1098 	}
1099 	else fi=atof(pkt2)*PI/180.;
1100 
1101         if (v[0]=='x')
1102         {
1103           nslave[i].ny=y*cos(fi)-z*sin(fi);
1104           nslave[i].nz=y*sin(fi)+z*cos(fi);
1105         }
1106         else if (v[0]=='y')
1107         {
1108           nslave[i].nz=z*cos(fi)-x*sin(fi);
1109           nslave[i].nx=z*sin(fi)+x*cos(fi);
1110         }
1111         else if (v[0]=='z')
1112         {
1113           nslave[i].nx=x*cos(fi)-y*sin(fi);
1114           nslave[i].ny=x*sin(fi)+y*cos(fi);
1115         }
1116         else
1117         { errMsg(" rotational Axis not known:%1s \n", v); return(-1); }
1118       }
1119     }
1120     else
1121     {
1122       fi = val[0]*PI/180.;
1123       pnr1=getPntNr( pkt1 );
1124       pnr2=getPntNr( pkt2 );
1125       if (pnr1<0)
1126       {
1127         errMsg(" ERROR: Point1 (%s) is undefined\n", pkt1 );
1128         return(-1);
1129       }
1130       else
1131       {
1132         p1[0] = point[pnr1].px;
1133         p1[1] = point[pnr1].py;
1134         p1[2] = point[pnr1].pz;
1135       }
1136       if (pnr2<0)
1137       {
1138         if(compareStrings(pkt2,"x")>0) v_add(p1, ex, p2);
1139         else if(compareStrings(pkt2,"y")>0) v_add(p1, ey, p2);
1140         else if(compareStrings(pkt2,"z")>0) v_add(p1, ez, p2);
1141         else { errMsg(" ERROR: Point2 (%s) is undefined\n", pkt2 );        return(-1); }
1142       }
1143       else
1144       {
1145         p2[0] = point[pnr2].px;
1146         p2[1] = point[pnr2].py;
1147         p2[2] = point[pnr2].pz;
1148       }
1149 
1150       /* berechnung der Einheitsvektoren des Verdreh-Koordinatensystems */
1151       /* Exneu = p1p2/|p1p2| ist der Einheitsvektor in xneu richtung    */
1152       /* Eyneu = p1p2 X p1ph / |p1p2 X p1ph|     in yneu                */
1153       /* Ezneu = Exneu X Eyneu                   in zneu                */
1154 
1155       v_result( p1, p2, p1p2 );
1156       v_norm  ( p1p2, el );
1157 
1158       /* erzeuge einen Hilfspunkt der nicht auf der el-achse liegt  */
1159       ph[1] = p1p2[0];
1160       ph[2] = p1p2[1];
1161       ph[0] = p1p2[2];
1162 
1163       /* konstuiere damit den 2. einheitsvektor eh  */
1164       v_result( p1, ph, p1ph );
1165       v_prod( p1p2, p1ph, ph );
1166       v_norm (ph, eh);
1167 
1168       /* und der dritte: eq  */
1169       v_prod( p1p2, ph, ex );
1170       v_norm (ex, eq);
1171 
1172       /* berechnung der lhq-koordinaten der xyz einheitsvektoren durch zykl.vertausch.  */
1173       ex[0]=el[0];
1174       ex[1]=eh[0];
1175       ex[2]=eq[0];
1176 
1177       ey[0]=el[1];
1178       ey[1]=eh[1];
1179       ey[2]=eq[1];
1180 
1181       ez[0]=el[2];
1182       ez[1]=eh[2];
1183       ez[2]=eq[2];
1184 
1185       /* Berechnung der lhq-koordinaten der Drehachse (offset fuer die Drehung) */
1186       x=point[pnr1].px*scale->w+scale->x;
1187       y=point[pnr1].py*scale->w+scale->y;
1188       z=point[pnr1].pz*scale->w+scale->z;
1189 
1190       /* l=ex[0]*x+ey[0]*y+ez[0]*z; */
1191       h_offs=ex[1]*x+ey[1]*y+ez[1]*z;
1192       q_offs=ex[2]*x+ey[2]*y+ez[2]*z;
1193 
1194       /* Berechnung der lhq-koordinaten aller zu drehenden punkte */
1195       for (i=0; i<anz_n; i++)
1196       {
1197         x=nslave[i].nx;
1198         y=nslave[i].ny;
1199         z=nslave[i].nz;
1200         l=ex[0]*x+ey[0]*y+ez[0]*z;
1201         h=( ex[1]*x+ey[1]*y+ez[1]*z ) - h_offs ;
1202         q=( ex[2]*x+ey[2]*y+ez[2]*z ) - q_offs ;
1203 
1204         /* drehe um l  */
1205         dh=h*cos(fi)-q*sin(fi);
1206         dq=h*sin(fi)+q*cos(fi);
1207         dh+= h_offs;
1208         dq+= q_offs;
1209 
1210         nslave[i].nx=el[0]*l+eh[0]*dh+eq[0]*dq;
1211         nslave[i].ny=el[1]*l+eh[1]*dh+eq[1]*dq;
1212         nslave[i].nz=el[2]*l+eh[2]*dh+eq[2]*dq;
1213       }
1214     }
1215     return(3);
1216   }
1217   else if(( compare(type,"mir",3) == 3 )||( compare(type,"MIR",3) == 3 ))
1218   {
1219     /* the "mirror" is placed at p2 and is perpendicular to p1p2 */
1220     length=sscanf( record, "%*s%s%s", pkt1, pkt2 );
1221     pnr1=getPntNr( pkt1 );
1222     if(length==2) pnr2=getPntNr( pkt2 ); else pnr2=-1;
1223     if (pnr2<0) pnr2=-1;
1224 
1225     if (pnr1<0)
1226     {
1227       pnr2=-2;
1228       p1[0] = 0.;
1229       p1[1] = 0.;
1230       p1[2] = 0.;
1231       if(pkt1[0]=='x')
1232       {
1233         v_add(p1, ex, p2);
1234       }
1235       else if(pkt1[0]=='y')
1236       {
1237         v_add(p1, ey, p2);
1238       }
1239       else if(pkt1[0]=='z')
1240       {
1241         v_add(p1, ez, p2);
1242       }
1243       else { errMsg(" ERROR: Point (%s) is undefined\n", pkt1 ); return(-1); }
1244 
1245       /* switch p1 p2 */
1246       for(i=0; i<3; i++) { p1p2[i]=p1[i]; p1[i]=p2[i]; p2[i]=p1p2[i]; }
1247     }
1248     else
1249     {
1250       p1[0] = point[pnr1].px*scale->w+scale->x;
1251       p1[1] = point[pnr1].py*scale->w+scale->y;
1252       p1[2] = point[pnr1].pz*scale->w+scale->z;
1253     }
1254 
1255     if (pnr2==-1)
1256     {
1257       if(pkt2[0]=='x')
1258       {
1259         v_add(p1, ex, p2);
1260       }
1261       else if(pkt2[0]=='y')
1262       {
1263         v_add(p1, ey, p2);
1264       }
1265       else if(pkt2[0]=='z')
1266       {
1267         v_add(p1, ez, p2);
1268       }
1269       else { errMsg(" ERROR: Point (%s) is undefined\n", pkt2 ); return(-1); }
1270 
1271       /* switch p1 p2 */
1272       for(i=0; i<3; i++) { p1p2[i]=p1[i]; p1[i]=p2[i]; p2[i]=p1p2[i]; }
1273     }
1274     else if (pnr2>=0)
1275     {
1276       p2[0] = point[pnr2].px*scale->w+scale->x;
1277       p2[1] = point[pnr2].py*scale->w+scale->y;
1278       p2[2] = point[pnr2].pz*scale->w+scale->z;
1279     }
1280 
1281     /* calculation of the first mirrored point (modification of p2) */
1282     v_result( p1, p2, p1p2 );
1283     l=2.;
1284     v_scal( &l, p1p2, p1p2 );
1285     v_add( p1,  p1p2, p2 );
1286 
1287     ph[0] = p2[0]+p1[0];
1288     ph[1] = p2[1]+p1[1];
1289     ph[2] = p2[2]+p1[2];
1290 
1291     fi=v_sprod(p1p2, p1p2);
1292 
1293     for (i=0; i<anz_n; i++)
1294     {
1295       el[0]=(ph[0]-2*nslave[i].nx) ;
1296       el[1]=(ph[1]-2*nslave[i].ny) ;
1297       el[2]=(ph[2]-2*nslave[i].nz) ;
1298       l=v_sprod(el, p1p2);
1299       h=l/fi;
1300       nslave[i].nx+= h*p1p2[0];
1301       nslave[i].ny+= h*p1p2[1];
1302       nslave[i].nz+= h*p1p2[2];
1303     }
1304     return(5);
1305   }
1306   else
1307   {
1308     printf(" ERROR: transformation not recognized: %s\n", type );
1309     return(-1);
1310   }
1311 }
1312 
1313 /* calculate the necessary division of the line with respect to the curvature */
calcLineDiv(Lines * line,int nr,double gtol_cos_a,double lmax,double lmin)1314 int calcLineDiv(Lines *line, int nr, double gtol_cos_a, double lmax, double lmin)
1315 {
1316   int i,imin=0,imax=0,sig=0;
1317 
1318   double e0[3], e1[3], v0[3], v1[3],bias=1;
1319   double l, cos_a, min_cos_a=MAX_FLOAT, max_cos_a=0., min_cos_a_buf;
1320 
1321   /* generate the initial drawing and potential meshing points (called points now) on the line */
1322 
1323   /* start with a high div and relax it as long as the requirements are fullfilled */
1324   line[nr].div=MAX_LINE_DIV-(MAX_LINE_DIV%2);
1325   line[nr].bias=1.;
1326 
1327   /* change the div until lmax is reached */
1328  new_div:;
1329   repLine(nr);
1330   v_result(&line[nr].ip[0],&line[nr].ip[3], v0);
1331   l=v_betrag(v0);
1332   if(l>=lmax)
1333   {
1334     line[nr].div=1+(double)line[nr].div*l/lmax;
1335     goto new_div;
1336   }
1337 
1338   /* calculate the angle between vectors from 1st point to second and to 3rd, over all points */
1339  new_bia:;
1340   imin=imax=sig=0;
1341   if (min_cos_a<MAX_FLOAT) min_cos_a_buf=min_cos_a; else min_cos_a_buf=0.;
1342   for(i=0; i<line[nr].nip-6; i+=3)
1343   {
1344     v_result(&line[nr].ip[i],&line[nr].ip[i+3], v0);
1345     v_result(&line[nr].ip[i],&line[nr].ip[i+6], v1);
1346     v_norm(v0, e0);
1347     v_norm(v1, e1);
1348     cos_a= v_sprod(e0, e1);
1349     if(cos_a<min_cos_a) { min_cos_a=cos_a; imin=i; }
1350     if(cos_a>max_cos_a) { max_cos_a=cos_a; imax=i; }
1351   }
1352 
1353   /* change the bia as long as min_cos_a increases and  min_cos_a/max_cos_a gets smaller */
1354   //if(i) printf(" i:%d cos_a %f %f %f a:%f line[nr].bias:%f \n",i,min_cos_a_buf,min_cos_a,max_cos_a,asin(max_cos_a-min_cos_a)*180.,line[nr].bias );
1355   if(i)
1356   {
1357     //printf("%s max_cos_a-min_cos_a %f g:%f\n",line[nr].name, max_cos_a-min_cos_a,MIN_DCOS_A);
1358     // is the angle difference significant?
1359     if((max_cos_a-min_cos_a)>MIN_DCOS_A)
1360     {
1361       //printf("imin:%d i/3:%d i*2/3:%d\n", imin,i/3,i*2/3);
1362       // is the maximum curvature at the borders?
1363       if((imin<i/3)||(imin>i*2/3))
1364       {
1365         //printf("cos_a buf %f %f\n",min_cos_a_buf,min_cos_a);
1366         // decreases the maximum angle?
1367         if(min_cos_a>min_cos_a_buf)
1368 	{
1369           bias*=2;
1370           if(imin>imax)  // greater curvature at line end, smaller elem at end
1371           {
1372             sig=-1;
1373             line[nr].bias= 1./pow((bias), (1./((double)line[nr].div-1.)));
1374           }
1375           else
1376           {
1377             sig=1;
1378             line[nr].bias=pow(bias, (1./((double)line[nr].div-1.)));
1379           }
1380           //printf(" bia:%f %f\n",bias);
1381           repLine(nr);
1382           goto new_bia;
1383         }
1384       }
1385     }
1386   }
1387 
1388 
1389   /* get the length between points */
1390   if(!i) /* straight line, no inner points */
1391   {
1392     min_cos_a=1;
1393     v_result(&line[nr].ip[i],&line[nr].ip[i+3], v0);
1394     l=v_betrag(v0)/line[nr].div;
1395   }
1396   else
1397   {
1398     /* Calculate the length between points. It is sufficient to do it for the last sector */
1399     l=v_betrag(v0);
1400   }
1401 
1402   /* change the div */
1403   /* if the minimum cos_a in the line is greater than the target and the length is smaller as lmax reduce the division by factor of 2 */
1404   if((min_cos_a>gtol_cos_a)&&(line[nr].div>MIN_LINE_DIV)&&(l<lmax))
1405   {
1406     //printf("l:%f div:%d\n",l,line[nr].div);
1407     if(l*2<lmax) { line[nr].div/=2; bias=1.; line[nr].bias=1.; min_cos_a=MAX_FLOAT; max_cos_a=0.; goto new_div; }
1408     else line[nr].div=(double)line[nr].div*l/lmax;
1409   }
1410 
1411   /* check the division if the elements would be too small */
1412   if(l<=lmin)
1413   {
1414     line[nr].div=(double)line[nr].div*l/lmin;
1415   }
1416 
1417   if(line[nr].div<1) line[nr].div=MIN_LINE_DIV;
1418   if((MIN_LINE_DIV%2==0)&&(line[nr].div%2!=0)) line[nr].div+=1;
1419 
1420   /* final bias adjust */
1421   if(sig==-1)  // greater curvature at line end, smaller elem at end
1422   {
1423     line[nr].bias= 1./pow((bias), (1./((double)line[nr].div-1.)));
1424   }
1425   else if(sig==1)
1426   {
1427     line[nr].bias=pow(bias, (1./((double)line[nr].div-1.)));
1428   }
1429   else line[nr].bias=1;
1430 
1431   if(printFlag) printf("line:%s div:%d bias:%f gtol_cos_a:%lf min_cos_a:%lf lmax:%lf l:%lf\n", line[nr].name, line[nr].div, line[nr].bias, gtol_cos_a, min_cos_a, lmax*scale->w, l*scale->w);
1432 
1433   return(line[nr].div);
1434 }
1435 
1436 
1437 
1438 
repNurl(int nr)1439 void repNurl(int nr )
1440 {
1441   int i,j;
1442 
1443   /* calculation of the controll-point-array for drawing purposes */
1444   if( nurbl[nr].name != (char *)NULL )
1445   {
1446     if( (nurbl[nr].ctlarray = (GLfloat *)realloc( (GLfloat *)nurbl[nr].ctlarray, (nurbl[nr].u_npnt*nurbl[nr].u_stride)*sizeof(GLfloat) )) == NULL )
1447     { printf(" ERROR: realloc failure in repNurbl(), nurbl:%s can not be shaped\n\n", nurbl[nr].name);
1448       return; }
1449 
1450     /* calculate the position of the controll-array */
1451     j=0; for (i=0; i<nurbl[nr].u_npnt; i++ )
1452     {
1453       nurbl[nr].ctlarray[j++]=nurbl[nr].weight[i]*(GLfloat)point[nurbl[nr].ctlpnt[i]].px ;
1454       nurbl[nr].ctlarray[j++]=nurbl[nr].weight[i]*(GLfloat)point[nurbl[nr].ctlpnt[i]].py ;
1455       nurbl[nr].ctlarray[j++]=nurbl[nr].weight[i]*(GLfloat)point[nurbl[nr].ctlpnt[i]].pz ;
1456       nurbl[nr].ctlarray[j++]=nurbl[nr].weight[i] ;
1457     }
1458   }
1459 }
1460 
1461 
calcNurbsResolution(int nr)1462 void calcNurbsResolution( int nr)
1463 {
1464   int i;
1465   double p0[3], p0p1[3], p1[3];
1466   double umin, umax,du,vmin,vmax,dv, lu, lv;
1467   Points tab_p[UV_STEPS+1];
1468   double dtab_u[UV_STEPS+1], dtab_v[UV_STEPS+1], dtab_05[UV_STEPS+1];
1469 
1470   sem_wait(&sem_g);
1471 #if TEST
1472   printf("calcNurbsResolution-nurbs:%s \n", nurbs[nr].name);
1473 #endif
1474   /* calculation of the u,v resolution based on real world scale */
1475   /* get the u and v range by looking into the knots */
1476   umin=nurbs[nr].uknt[0];
1477   umax=nurbs[nr].uknt[nurbs[nr].u_nknt-1];
1478   vmin=nurbs[nr].vknt[0];
1479   vmax=nurbs[nr].vknt[nurbs[nr].v_nknt-1];
1480   sem_post(&sem_g);
1481   du=(umax-umin)/(UV_STEPS-1);
1482   dv=(vmax-vmin)/(UV_STEPS-1);
1483   if( (printFlag) && ((umin<0.)||(vmin<0.)) )
1484   {
1485     printf(" WARNING: in NURBS:%s umin or vmin negative! They are set to 0. in evalNurbs()\n", nurbs[nr].name);
1486     printf(" umin:%lf umax:%lf du:%lf\n", umin,umax,du);
1487     printf(" vmin:%lf vmax:%lf dv:%lf\n", vmin,vmax,dv);
1488   }
1489   for(i=0; i<UV_STEPS; i++) { dtab_u[i]=umin+du*i; dtab_v[i]=vmin+dv*i; }
1490 
1491   for(i=0; i<UV_STEPS; i++) { dtab_05[i]=(vmax+vmin)*.5; }
1492   evalNurbs( nr, UV_STEPS, dtab_u, dtab_05, tab_p);
1493   lu=0.;
1494   for(i=1; i<UV_STEPS; i++)
1495   {
1496     p0[0]=tab_p[i-1].px;
1497     p0[1]=tab_p[i-1].py;
1498     p0[2]=tab_p[i-1].pz;
1499     p1[0]=tab_p[i].px;
1500     p1[1]=tab_p[i].py;
1501     p1[2]=tab_p[i].pz;
1502     v_result(p0,p1,p0p1);
1503     lu+=v_betrag(p0p1);
1504   }
1505 
1506   for(i=0; i<UV_STEPS; i++) { dtab_05[i]=(umax+umin)*.5; }
1507   evalNurbs( nr, UV_STEPS, dtab_05, dtab_v, tab_p);
1508   lv=0.;
1509   for(i=1; i<UV_STEPS; i++)
1510   {
1511     p0[0]=tab_p[i-1].px;
1512     p0[1]=tab_p[i-1].py;
1513     p0[2]=tab_p[i-1].pz;
1514     p1[0]=tab_p[i].px;
1515     p1[1]=tab_p[i].py;
1516     p1[2]=tab_p[i].pz;
1517     v_result(p0,p1,p0p1);
1518     lv+=v_betrag(p0p1);
1519   }
1520 
1521   /* average resolution */
1522   sem_wait(&sem_g);
1523   nurbs[nr].ures=lu/(umax-umin);
1524   nurbs[nr].vres=lv/(vmax-vmin);
1525   sem_post(&sem_g);
1526 }
1527 
1528 
1529 
repNurs(int nr)1530 void repNurs(int nr )
1531 {
1532   int i,j;
1533 
1534   if( nurbs[nr].name == (char *)NULL ) return;
1535 
1536   /* calculation of an average position for the name-string */
1537   nurbs[nr].tx=nurbs[nr].ty=nurbs[nr].tz=0.;
1538   for (i=0; i<nurbs[nr].u_npnt; i++)
1539   {
1540     for (j=0; j<nurbs[nr].v_npnt; j++)
1541     {
1542       nurbs[nr].tx+=point[nurbs[nr].ctlpnt[i][j]].px ;
1543       nurbs[nr].ty+=point[nurbs[nr].ctlpnt[i][j]].py ;
1544       nurbs[nr].tz+=point[nurbs[nr].ctlpnt[i][j]].pz ;
1545     }
1546   }
1547   nurbs[nr].tx/=nurbs[nr].u_npnt*nurbs[nr].v_npnt;
1548   nurbs[nr].ty/=nurbs[nr].u_npnt*nurbs[nr].v_npnt;
1549   nurbs[nr].tz/=nurbs[nr].u_npnt*nurbs[nr].v_npnt;
1550 
1551   /* calculation of the controll-point-array for drawing purposes */
1552   if( nurbs[nr].name != (char *)NULL )
1553   {
1554     if( (nurbs[nr].ctlarray = (GLfloat *)realloc( (GLfloat *)nurbs[nr].ctlarray, (nurbs[nr].u_npnt*nurbs[nr].v_npnt*nurbs[nr].v_stride+5)*sizeof(GLfloat) )) == NULL )
1555     { printf(" ERROR: realloc failure in repNurbs(), nurbs:%s can not be shaped\n\n", nurbs[nr].name);
1556       return; }
1557 
1558     /* calculate the position of the control-array */
1559     for (i=0; i<nurbs[nr].u_npnt; i++)
1560     {
1561       for (j=0; j<nurbs[nr].v_npnt; j++)
1562       {
1563         nurbs[nr].ctlarray[i*(nurbs[nr].v_stride*nurbs[nr].v_npnt)+j*(nurbs[nr].v_stride)+0]=nurbs[nr].weight[i][j]*point[nurbs[nr].ctlpnt[i][j]].px;
1564         nurbs[nr].ctlarray[i*(nurbs[nr].v_stride*nurbs[nr].v_npnt)+j*(nurbs[nr].v_stride)+1]=nurbs[nr].weight[i][j]*point[nurbs[nr].ctlpnt[i][j]].py;
1565         nurbs[nr].ctlarray[i*(nurbs[nr].v_stride*nurbs[nr].v_npnt)+j*(nurbs[nr].v_stride)+2]=nurbs[nr].weight[i][j]*point[nurbs[nr].ctlpnt[i][j]].pz;
1566         nurbs[nr].ctlarray[i*(nurbs[nr].v_stride*nurbs[nr].v_npnt)+j*(nurbs[nr].v_stride)+3]=nurbs[nr].weight[i][j];
1567       }
1568     }
1569   }
1570 }
1571 
1572 
1573 
1574 /* delete the trimming data which where created in repSurf before */
untrimNurs(int nr)1575 void untrimNurs(int nr )
1576 {
1577   int patch, curve;
1578 
1579   if( nurbs[nr].name == (char *)NULL ) return;
1580 
1581   for(patch=0; patch<nurbs[nr].patches; patch++)
1582   {
1583     for(curve=0; curve<nurbs[nr].nc[patch]; curve++)
1584     {
1585       free(nurbs[nr].uv[patch][curve]);
1586       free(nurbs[nr].xyz[patch][curve]);
1587     }
1588     free(nurbs[nr].np[patch]);
1589     free(nurbs[nr].uv[patch]);
1590     free(nurbs[nr].xyz[patch]);
1591   }
1592   if(!nurbs[nr].np)  free(nurbs[nr].np);
1593   if(!nurbs[nr].uv)  free(nurbs[nr].uv);
1594   if(!nurbs[nr].xyz) free(nurbs[nr].xyz);
1595   if(!nurbs[nr].nc)  free(nurbs[nr].nc);
1596   if(!nurbs[nr].vmax) free(nurbs[nr].vmax);
1597   if(!nurbs[nr].umax) free(nurbs[nr].umax);
1598   if(!nurbs[nr].vstep) free(nurbs[nr].vstep);
1599   if(!nurbs[nr].ustep) free(nurbs[nr].ustep);
1600 
1601   nurbs[nr].np=NULL;
1602   nurbs[nr].uv=NULL;
1603   nurbs[nr].xyz=NULL;
1604 
1605   nurbs[nr].nc=NULL;
1606   nurbs[nr].vmax=NULL;
1607   nurbs[nr].umax=NULL;
1608   nurbs[nr].vstep=NULL;
1609   nurbs[nr].ustep=NULL;
1610 
1611   nurbs[nr].patches=0;
1612 }
1613 
1614 
1615 /* return 0: node hidden, 1:node visible */
plotNode(int nr)1616 int plotNode(int nr)
1617 {
1618   extern char drawMode;
1619   extern double *colNr;
1620 
1621   int j,hits;
1622   int node_zmin;
1623   GLint viewport[4];
1624   GLdouble mvmatrix[16], projmatrix[16];
1625 
1626   static GLdouble wx, wy, wz;  /*  returned world x, y, z coords  */
1627   GLuint size_selectBuf;
1628   static GLuint *selectBuf=NULL;
1629   double dum;
1630 
1631   dum=(double)anz->n+anz->e+anz->f+anz->g+anz->t+anzGeo->p+anzGeo->l+anzGeo->s+anzGeo->b+anzGeo->sh+anzGeo->nurl+anzGeo->nurs+1000;
1632   dum*=10;
1633   if(dum > MAX_INTEGER) size_selectBuf=MAX_INTEGER;
1634   else size_selectBuf=(GLuint)dum;
1635   do{
1636     if( ( selectBuf= (GLuint *)realloc((GLuint *)selectBuf, size_selectBuf * sizeof(GLuint))) == NULL )
1637     {
1638       printf ("WARNING: in Pick() is size_selectBuf: %d to large and is reduced\n", size_selectBuf);
1639       size_selectBuf/=2;
1640     }
1641     if(size_selectBuf<100)
1642     {
1643       errMsg("\n\n ERROR: realloc Failure in plotNode()\n\n") ;
1644       return(0);
1645     }
1646   }while(!selectBuf);
1647 
1648   glutSetWindow( w1);
1649   glGetIntegerv (GL_VIEWPORT, viewport);
1650   glSelectBuffer (size_selectBuf, selectBuf);
1651 
1652   /* get the window location of the node to specify the picking rectangle */
1653   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
1654   glLoadIdentity();
1655   moveModel();
1656   //glGetIntegerv (GL_VIEWPORT, viewport);
1657   glGetDoublev (GL_MODELVIEW_MATRIX, mvmatrix);
1658   glGetDoublev (GL_PROJECTION_MATRIX, projmatrix);
1659   if (gluProject( node[nr].nx, node[nr].ny, node[nr].nz, mvmatrix, projmatrix, viewport,  &wx, &wy, &wz)==GL_FALSE)
1660   {
1661     printf("WARNING: Malfunction in plotNode()\n");
1662     return(0);
1663   }
1664 
1665   /* get the min_z value of the node */
1666   glRenderMode (GL_SELECT);
1667   glInitNames();
1668   glPushName (-1);
1669   glLoadIdentity();
1670   gluPickMatrix( (GLdouble) wx, (GLdouble) wy, 1,1, viewport);
1671   moveModel();
1672   glColor3d ( 0.5,0.5,0.5 );
1673   glPointSize(10);
1674   glLoadName('n');
1675     glPushName(nr);
1676     glBegin ( GL_POINTS );
1677     glVertex3dv ( &node[nr].nx );
1678     glEnd();
1679     glPopName();
1680   glLoadName(-1);
1681   glFlush();
1682   hits = glRenderMode (GL_RENDER);
1683   if (hits<0)
1684   {
1685     errMsg("\nWARNING: Overflow occured!\n");
1686     free(selectBuf); selectBuf=NULL;
1687     return(0);
1688   }
1689   else
1690   {
1691     processHits( hits, selectBuf, "a   ", "a", wx, wy );
1692     node_zmin=pick_zmin;
1693   }
1694 
1695   /* get the min_z value of all other displayes entities */
1696   //glGetIntegerv (GL_VIEWPORT, viewport);
1697   glRenderMode (GL_SELECT);
1698   glInitNames();
1699   glPushName (-1);
1700   glLoadIdentity();
1701   gluPickMatrix( (GLdouble) wx, (GLdouble) wy, 1,1, viewport);
1702   moveModel();
1703   if (drawMode==4)
1704   {
1705     drawSets( PICK );
1706   }
1707   else
1708   {
1709     for (j=0; j<anzGeo->psets; j++ )
1710     {
1711       if(pset[j].type[0]=='e') drawElements_plot( set[pset[j].nr].anz_e, set[pset[j].nr].elem, node, colNr, e_enqire, 2, 0,1,PICK );
1712       if(pset[j].type[0]=='f') drawFaces_plot( set[pset[j].nr].anz_f, set[pset[j].nr].face, node, colNr, face, 2, 0, 1, PICK );
1713     }
1714   }
1715 
1716   glFlush();
1717   hits = glRenderMode (GL_RENDER);
1718   if (hits<0)
1719   {
1720     errMsg("\nWARNING: Overflow occured!\n");
1721     free(selectBuf); selectBuf=NULL;
1722     return(0);
1723   }
1724   else
1725   {
1726     processHits( hits, selectBuf, "a   ", "a", wx, wy );
1727     free(selectBuf); selectBuf=NULL;
1728     if((unsigned int)node_zmin<=(unsigned int)pick_zmin) return(1); else return(0);
1729   }
1730 }
1731 
1732 
1733 
adjustFeedBack(int npgn,GLfloat * pgn,double ** ptr)1734 int adjustFeedBack( int npgn, GLfloat *pgn, double **ptr)
1735 {
1736   int i, n=0, m=0, nv, nbuf;
1737   double v1[3],v2[3],vn[3];
1738   double vbuf[2][3];
1739 
1740   GLfloat token;
1741   double *pgn_new;
1742 
1743   /* create a new array for the polygons with its normals (guessed to be twice as large) */
1744   if( (pgn_new= (double *)malloc( 2*sizeof(double) )) == NULL )
1745   { printf(" ERROR: malloc failure in adjustFeedBack()\n\n"); exit(-1); }
1746 
1747 
1748   while((npgn-n))
1749   {
1750     token= pgn[n++];
1751     if(token==GL_POLYGON_TOKEN)
1752     {
1753       pgn_new[m++]=token;
1754       nv=pgn_new[m++]=pgn[n++];
1755       if(nv!=3) printf("GL_POLYGON_TOKEN with %d vertexes\n", nv);
1756       if( (pgn_new= (double *)realloc((double *)pgn_new, (m+nv*6+2)*sizeof(double) )) == NULL )
1757       { printf(" ERROR: malloc failure in adjustFeedBack()\n\n"); exit(-1); }
1758       nbuf=n;
1759       for( i=0; i<nv; i++)
1760       {
1761         pgn[n]=((double)pgn[n]*2./(double)width_w1-1.)*aspectRatio_w1; n++;  /* x */
1762         pgn[n]=(double)pgn[n]*2/(double)width_w1*aspectRatio_w1-1.; n++; /* y */
1763         pgn[n]-=0.5;  pgn[n++]*=-2.;   /* z */
1764       }
1765 
1766       /* calculate the normal and add to the start of the polygon */
1767       vbuf[0][0]=pgn[n-nv*3];
1768       vbuf[0][1]=pgn[n-nv*3+1];
1769       vbuf[0][2]=pgn[n-nv*3+2];
1770       vbuf[1][0]=pgn[3+n-nv*3];
1771       vbuf[1][1]=pgn[3+n-nv*3+1];
1772       vbuf[1][2]=pgn[3+n-nv*3+2];
1773       v_result(vbuf[0],vbuf[1], v1);
1774       vbuf[1][0]=pgn[6+n-nv*3];
1775       vbuf[1][1]=pgn[6+n-nv*3+1];
1776       vbuf[1][2]=pgn[6+n-nv*3+2];
1777       v_result(vbuf[0],vbuf[1], v2);
1778       v_prod(v1,v2,vn);
1779       v_norm(vn,&pgn_new[m]); m+=3;
1780       for( i=0; i<nv; i++)
1781       {
1782         pgn_new[m++]=pgn[nbuf++];  /* x */
1783         pgn_new[m++]=pgn[nbuf++];  /* y */
1784         pgn_new[m++]=pgn[nbuf++];  /* z */
1785       }
1786 
1787     }
1788     else if(token==GL_POINT_TOKEN) { printf("ERROR: unsupported token %lf\n",token ); exit(-1); }
1789     else if(token==GL_LINE_TOKEN) { printf("ERROR: unsupported token %lf\n",token ); exit(-1); }
1790     else if(token==GL_LINE_RESET_TOKEN) { printf("ERROR: unsupported token %lf\n",token ); exit(-1); }
1791     else if(token==GL_PASS_THROUGH_TOKEN) { printf("ERROR: unsupported token %lf\n",token ); exit(-1); }
1792     else { printf("ERROR: unknown token %lf\n",token ); exit(-1); }
1793   }
1794 
1795   *ptr=pgn_new;
1796   return(m);
1797 }
1798 
1799 
1800 
fillBlendedSurf(int nr)1801 int fillBlendedSurf(int nr)
1802 {
1803   int i,j,s;
1804   int setNr;
1805 
1806   int anz_p=0, anz_l=0, anz_s=0, newSetFlag=0;
1807   Points *ptmp=NULL;
1808   Lines *ltmp=NULL;
1809   Gsur *stmp=NULL;
1810 
1811   /* it is not allowed to run fillBlendedSurf in parallel (reason: set BLR would have to be used by all instances, one set per thread needed ) */
1812   if(sem_wait(&sem_rep)) printf("Error in:sem_wait\n");
1813 
1814   /* store surf in set */
1815   setNr=getSetNr(specialset->blr);
1816   if(setNr<0)
1817   {
1818     if(printFlag) printf(" WARNING in fillBlendedSurf(), set:%s does not exist and is initialized\n",specialset->blr);
1819     /* no threading environment, gen a set */
1820     setNr=pre_seta(specialset->blr, "i", 0);
1821     newSetFlag=1;
1822   }
1823   sem_wait(&sem_g);
1824   seta(setNr,"s",nr);
1825 
1826   /* complete set */
1827   completeSet( specialset->blr, "do") ;
1828   sem_post(&sem_g);
1829 
1830   /* save the mesh data of the affected entities */
1831   if ((ptmp = (Points *)malloc((set[setNr].anz_p+1)*sizeof(Points)) ) == NULL )
1832   { printf("\n ERROR: malloc failure\n\n"); return(-1); }
1833   anz_p=set[setNr].anz_p;
1834   for(i=0; i<set[setNr].anz_p; i++)
1835   {
1836     if ((ptmp[i].nod = (int *)malloc((int)(point[set[setNr].pnt[i]].nn+1)*sizeof(int)) ) == NULL )
1837     { printf(" ERROR: malloc failure\n\n"); return(-1);  }
1838     for(j=0; j<point[set[setNr].pnt[i]].nn; j++)
1839     {
1840       ptmp[i].nod[j]=point[set[setNr].pnt[i]].nod[j];
1841     }
1842     ptmp[i].nn=point[set[setNr].pnt[i]].nn;
1843     free(point[set[setNr].pnt[i]].nod);
1844     point[set[setNr].pnt[i]].nod=NULL;
1845     point[set[setNr].pnt[i]].nn=0;
1846   }
1847   if ((ltmp = (Lines  *)malloc((set[setNr].anz_l+1)*sizeof(Lines )) ) == NULL )
1848   { printf("\n ERROR: malloc failure\n\n"); return(-1); }
1849   anz_l=set[setNr].anz_l;
1850   for(i=0; i<set[setNr].anz_l; i++)
1851   {
1852     if ((ltmp[i].nod = (int *)malloc((int)(line[set[setNr].line[i]].nn+1)*sizeof(int)) ) == NULL )
1853     { printf(" ERROR: malloc failure\n\n"); return(-1); }
1854     for(j=0; j<line[set[setNr].line[i]].nn; j++)
1855     {
1856       ltmp[i].nod[j]=line[set[setNr].line[i]].nod[j];
1857     }
1858     if ((ltmp[i].elem = (int *)malloc((int)(line[set[setNr].line[i]].ne+1)*sizeof(int)) ) == NULL )
1859     { printf(" ERROR: malloc failure\n\n"); return(-1); }
1860     for(j=0; j<line[set[setNr].line[i]].ne; j++)
1861     {
1862       ltmp[i].elem[j]=line[set[setNr].line[i]].elem[j];
1863     }
1864     ltmp[i].nn=line[set[setNr].line[i]].nn;
1865     ltmp[i].ne=line[set[setNr].line[i]].ne;
1866     ltmp[i].eattr=line[j].eattr;
1867     free(line[set[setNr].line[i]].nod); free(line[set[setNr].line[i]].elem);
1868     line[set[setNr].line[i]].nod=NULL; line[set[setNr].line[i]].elem=NULL;
1869     line[set[setNr].line[i]].nn=0; line[set[setNr].line[i]].ne=0;
1870   }
1871   if ((stmp = (Gsur   *)malloc((set[setNr].anz_s+1)*sizeof(Gsur  )) ) == NULL )
1872   { printf("\n ERROR: malloc failure\n\n"); return(-1); }
1873   anz_s=set[setNr].anz_s;
1874   for(i=0; i<set[setNr].anz_s; i++)
1875   {
1876     if ((stmp[i].nod = (int *)malloc((int)(surf[set[setNr].surf[i]].nn+1)*sizeof(int)) ) == NULL )
1877     { printf(" ERROR: malloc failure\n\n"); return(-1); }
1878     for(j=0; j<surf[set[setNr].surf[i]].nn; j++)
1879     {
1880       stmp[i].nod[j]=surf[set[setNr].surf[i]].nod[j];
1881     }
1882     if ((stmp[i].elem = (int *)malloc((int)(surf[set[setNr].surf[i]].ne+1)*sizeof(int)) ) == NULL )
1883     { printf(" ERROR: malloc failure\n\n");  return(-1);}
1884     for(j=0; j<surf[set[setNr].surf[i]].ne; j++)
1885     {
1886       stmp[i].elem[j]=surf[set[setNr].surf[i]].elem[j];
1887     }
1888     stmp[i].nn=surf[set[setNr].surf[i]].nn;
1889     stmp[i].ne=surf[set[setNr].surf[i]].ne;
1890     stmp[i].eattr=surf[j].eattr;
1891     surf[j].eattr=0;
1892     free(surf[set[setNr].surf[i]].nod); free(surf[set[setNr].surf[i]].elem);
1893     surf[set[setNr].surf[i]].nod=NULL; surf[set[setNr].surf[i]].elem=NULL;
1894     surf[set[setNr].surf[i]].nn=0; surf[set[setNr].surf[i]].ne=0;
1895   }
1896 
1897 
1898   /* mesh points and lines with the standard routines */
1899   /* create the rendering poligones with an modified routine without mesh-improver */
1900   meshPoints(setNr, 1) ;
1901   meshLines( setNr, 1);
1902   meshSurfs( setNr, 1) ;
1903 
1904   /* delete the temporary entities which were created to substitute 3- and 5-sided surfs */
1905   /* warning, s is now redefined */
1906   s=getSetNr(specialset->zap);
1907   if(s>-1)
1908   {
1909     for(i=0; i<set[s].anz_b; i++)
1910     {
1911 	if(printFlag) printf (" delete body:%s \n",  body[set[s].body[i]].name );
1912   sem_wait(&sem_g);
1913 	setr( 0, "b",set[s].body[i] );
1914   sem_post(&sem_g);
1915 	body[set[s].body[i]].name = (char *)NULL ;
1916 	body[set[s].body[i]].ns=0;
1917 	free(body[set[s].body[i]].o);
1918 	body[set[s].body[i]].o= NULL;
1919 	free(body[set[s].body[i]].s);
1920 	body[set[s].body[i]].s= NULL;
1921 	body[set[s].body[i]].nn=0;
1922 	free(body[set[s].body[i]].nod);
1923 	body[set[s].body[i]].nod= NULL;
1924 	body[set[s].body[i]].ne=0;
1925 	free(body[set[s].body[i]].elem);
1926 	body[set[s].body[i]].elem= NULL;
1927 	body[set[s].body[i]].etyp= 0;
1928     }
1929     for(i=0; i<set[s].anz_s; i++)
1930     {
1931 	if(printFlag) printf (" delete surf:%s \n",  surf[set[s].surf[i]].name );
1932   sem_wait(&sem_g);
1933 	setr( 0, "s",set[s].surf[i] );
1934   sem_post(&sem_g);
1935 	surf[set[s].surf[i]].name = (char *)NULL ;
1936 	surf[set[s].surf[i]].nl= 0;
1937 	free(surf[set[s].surf[i]].typ);
1938 	surf[set[s].surf[i]].typ= NULL;
1939 	free(surf[set[s].surf[i]].o);
1940 	surf[set[s].surf[i]].o= NULL;
1941 	free(surf[set[s].surf[i]].l);
1942 	surf[set[s].surf[i]].l= NULL;
1943 	surf[set[s].surf[i]].nn= 0;
1944 	free(surf[set[s].surf[i]].nod);
1945 	surf[set[s].surf[i]].nod= NULL;
1946 	surf[set[s].surf[i]].ne= 0;
1947 	free(surf[set[s].surf[i]].elem);
1948 	surf[set[s].surf[i]].elem= NULL;
1949 	surf[set[s].surf[i]].etyp= 0;
1950     }
1951     for(i=0; i<set[s].anz_l; i++)
1952     {
1953 	if(printFlag) printf (" delete line:%s \n",  line[set[s].line[i]].name );
1954   sem_wait(&sem_g);
1955 	setr( 0, "l",set[s].line[i] );
1956   sem_post(&sem_g);
1957 	line[set[s].line[i]].name = (char *)NULL ;
1958 	line[set[s].line[i]].div = 0;
1959 	if (line[set[s].line[i]].typ=='s')
1960 	{
1961 	  /* delete the set */
1962   sem_wait(&sem_g);
1963 	  delSet(set[line[set[s].line[i]].trk].name);
1964   sem_post(&sem_g);
1965 	}
1966 	line[set[s].line[i]].typ=' ';
1967 	line[set[s].line[i]].etyp=0;
1968 	line[set[s].line[i]].p1=-1;
1969 	line[set[s].line[i]].p2=-1;
1970 	line[set[s].line[i]].trk=-1;
1971 	line[set[s].line[i]].nip= 0;
1972 	free(line[set[s].line[i]].ip);
1973 	line[set[s].line[i]].ip= NULL;
1974 	line[set[s].line[i]].nn= 0;
1975 	free(line[set[s].line[i]].nod);
1976 	line[set[s].line[i]].nod = NULL;
1977 	line[set[s].line[i]].ne= 0;
1978 	free(line[set[s].line[i]].elem);
1979 	line[set[s].line[i]].elem = NULL;
1980     }
1981     for(i=0; i<set[s].anz_c; i++)
1982     {
1983 	if(printFlag) printf (" delete lcmb:%s \n",  lcmb[set[s].lcmb[i]].name );
1984   sem_wait(&sem_g);
1985 	setr( 0, "c",set[s].lcmb[i] );
1986   sem_post(&sem_g);
1987 	lcmb[set[s].lcmb[i]].name = (char *)NULL;
1988 	lcmb[set[s].lcmb[i]].nl=0;
1989 	free(lcmb[set[s].lcmb[i]].o);
1990 	lcmb[set[s].lcmb[i]].o= NULL;
1991 	free(lcmb[set[s].lcmb[i]].l);
1992 	lcmb[set[s].lcmb[i]].l= NULL;
1993 	lcmb[set[s].lcmb[i]].p1=-1;
1994 	lcmb[set[s].lcmb[i]].p2=-1;
1995     }
1996     for(i=0; i<set[s].anz_p; i++)
1997     {
1998 	if(printFlag) printf (" delete pnt:%s \n",  point[set[s].pnt[i]].name );
1999   sem_wait(&sem_g);
2000 	setr( 0, "p",set[s].pnt[i] );
2001   sem_post(&sem_g);
2002 	point[set[s].pnt[i]].name = (char *)NULL ;
2003 	free(point[set[s].pnt[i]].nod);
2004 	point[set[s].pnt[i]].nod=NULL;
2005 	point[set[s].pnt[i]].nn=0;
2006     }
2007     /* delete the set itself */
2008   sem_wait(&sem_g);
2009     delSet(specialset->zap);
2010   sem_post(&sem_g);
2011   }
2012 
2013   /* restore the mesh data of the affected entities */
2014   if(anz_p>set[setNr].anz_p) anz_p=set[setNr].anz_p;
2015   for(i=0; i<anz_p; i++)
2016   {
2017     point[set[setNr].pnt[i]].nn=ptmp[i].nn;
2018     if ((point[set[setNr].pnt[i]].nod = (int *)malloc((int)(point[set[setNr].pnt[i]].nn+1)*sizeof(int)) ) == NULL )
2019     { printf(" ERROR: malloc failure\n\n"); return(-1);  }
2020     for(j=0; j<ptmp[i].nn; j++)
2021     {
2022       point[set[setNr].pnt[i]].nod[j]=ptmp[i].nod[j];
2023     }
2024     free(ptmp[i].nod);
2025     ptmp[i].nod=NULL;
2026     ptmp[i].nn=0;
2027   }
2028   for(i=0; i<anz_l; i++)
2029   {
2030     line[set[setNr].line[i]].nn=ltmp[i].nn;
2031     line[set[setNr].line[i]].ne=ltmp[i].ne;
2032     if ((line[set[setNr].line[i]].nod = (int *)malloc((int)(line[set[setNr].line[i]].nn+1)*sizeof(int)) ) == NULL )
2033     { printf(" ERROR: malloc failure\n\n"); return(-1); }
2034     if ((line[set[setNr].line[i]].elem = (int *)malloc((int)(line[set[setNr].line[i]].ne+1)*sizeof(int)) ) == NULL )
2035     { printf(" ERROR: malloc failure\n\n"); return(-1); }
2036     for(j=0; j<ltmp[i].nn; j++)
2037     {
2038       line[set[setNr].line[i]].nod[j]=ltmp[i].nod[j];
2039     }
2040     for(j=0; j<ltmp[i].ne; j++)
2041     {
2042       line[set[setNr].line[i]].elem[j]=ltmp[i].elem[j];
2043     }
2044     free(ltmp[i].nod);
2045     ltmp[i].nod=NULL;
2046     ltmp[i].nn=0;
2047     free(ltmp[i].elem);
2048     ltmp[i].elem=NULL;
2049     ltmp[i].ne=0;
2050   }
2051   for(i=0; i<anz_s; i++)
2052   {
2053     surf[i].eattr=stmp[i].eattr;
2054     surf[set[setNr].surf[i]].nn=stmp[i].nn;
2055     surf[set[setNr].surf[i]].ne=stmp[i].ne;
2056     if (( surf[set[setNr].surf[i]].nod= (int *)malloc((int)(surf[set[setNr].surf[i]].nn+1)*sizeof(int)) ) == NULL )
2057     { printf(" ERROR: malloc failure\n\n"); return(-1); }
2058     if (( surf[set[setNr].surf[i]].elem= (int *)malloc((int)(surf[set[setNr].surf[i]].ne+1)*sizeof(int)) ) == NULL )
2059     { printf(" ERROR: malloc failure\n\n");  return(-1);}
2060     for(j=0; j<stmp[i].nn; j++)
2061     {
2062       surf[set[setNr].surf[i]].nod[j]=stmp[i].nod[j];
2063     }
2064     for(j=0; j<stmp[i].ne; j++)
2065     {
2066       surf[set[setNr].surf[i]].elem[j]=stmp[i].elem[j];
2067     }
2068     free(stmp[i].nod);
2069     stmp[i].nod=NULL;
2070     stmp[i].nn=0;
2071     free(stmp[i].elem);
2072     stmp[i].elem=NULL;
2073     stmp[i].ne=0;
2074   }
2075 
2076   sem_wait(&sem_g);
2077   if( newSetFlag==1) { if(printFlag) printf("del %s\n",specialset->blr); delSet(specialset->blr); }
2078   else
2079   {
2080     if(set[setNr].anz_elf)
2081      for(i=0; i<set[setNr].anz_elf; i++)
2082       if(set[setNr].elf[i].n) free(set[setNr].elf[i].v);
2083     free(set[setNr].valu);
2084     free(set[setNr].node);
2085     free(set[setNr].elem);
2086     free(set[setNr].face);
2087     free(set[setNr].elf);
2088     free(set[setNr].pnt);
2089     free(set[setNr].line);
2090     free(set[setNr].lcmb);
2091     free(set[setNr].surf);
2092     free(set[setNr].body);
2093     free(set[setNr].nurl);
2094     free(set[setNr].nurs);
2095     free(set[setNr].set);
2096     free(set[setNr].shp);
2097     free(set[setNr].eparm);
2098     set[setNr].valu=(int *)NULL;
2099     set[setNr].node=(int *)NULL;
2100     set[setNr].elem=(int *)NULL;
2101     set[setNr].face=(int *)NULL;
2102     set[setNr].elf=(Elfaces *)NULL;
2103     set[setNr].pnt= (int *)NULL;
2104     set[setNr].line=(int *)NULL;
2105     set[setNr].lcmb=(int *)NULL;
2106     set[setNr].surf=(int *)NULL;
2107     set[setNr].body=(int *)NULL;
2108     set[setNr].nurl=(int *)NULL;
2109     set[setNr].nurs=(int *)NULL;
2110     set[setNr].set=(int *)NULL;
2111     set[setNr].shp=(int *)NULL;
2112     set[setNr].eparm=(char *)NULL;
2113     set[setNr].material = -1;
2114     set[setNr].flag = 'c';
2115     set[setNr].type = 0;
2116     set[setNr].anz_v = 0;
2117     set[setNr].anz_n = 0;
2118     set[setNr].anz_e = 0;
2119     set[setNr].anz_f = 0;
2120     set[setNr].anz_elf = 0;
2121     set[setNr].anz_p = 0;
2122     set[setNr].anz_l = 0;
2123     set[setNr].anz_c = 0;
2124     set[setNr].anz_s = 0;
2125     set[setNr].anz_b = 0;
2126     set[setNr].anz_nurl = 0;
2127     set[setNr].anz_nurs = 0;
2128     set[setNr].anz_se = 0;
2129     set[setNr].anz_sh = 0;
2130   }
2131   sem_post(&sem_g);
2132   if(sem_post(&sem_rep)) printf("Error in:sem_post\n");
2133   return(1);
2134 }
2135 
2136 
_shapeToNurs(int s)2137 int _shapeToNurs(int s)
2138 {
2139   int pbuf[4], lbuf[4], sbuf,Sbuf;
2140   double l_offs;
2141   double p1[3], p2[3], el[3], eh[3], pa1[3], pa2[3], ps1[3], ps2[3], nr[3];
2142 
2143     printf("shape:%s\n", shape[s].name);
2144 
2145     /* generate 3 points on the shape which cover the drawing space */
2146     v_result( &point[shape[s].p[0]].px, &point[shape[s].p[1]].px, p1);
2147     v_result( &point[shape[s].p[0]].px, &point[shape[s].p[2]].px, p2);
2148 
2149     /* generate 2 perpendicular vectors in this shape */
2150     v_prod( p1, p2, nr );
2151     v_prod( nr, p1, p2 );
2152 
2153     v_norm( p1, el );
2154     v_norm( p2, eh );
2155 
2156     l_offs=2.;
2157     v_scal(&l_offs, el, p1);
2158     v_add(&point[shape[s].p[0]].px, p1, pa1);
2159     v_scal(&l_offs, eh, p1);
2160     v_add(&point[shape[s].p[0]].px, p1, ps1);
2161     l_offs=-2.;
2162     v_scal(&l_offs, el, p1);
2163     v_add(&point[shape[s].p[0]].px, p1, pa2);
2164     v_scal(&l_offs, eh, p1);
2165     v_add(&point[shape[s].p[0]].px, p1, ps2);
2166 
2167     pbuf[0]=pnt( "-pa1", pa1[0], pa1[1], pa1[2], 0 );
2168     pbuf[1]=pnt( "-pa2", pa2[0], pa2[1], pa2[2], 0 );
2169     pbuf[2]=pnt( "-ps1", ps1[0], ps1[1], ps1[2], 0 );
2170     pbuf[3]=pnt( "-ps2", ps2[0], ps2[1], ps2[2], 0 );
2171     lbuf[0]=line_i( "-1l", pbuf[0], pbuf[2], 0, 1, 1, 0 );
2172     lbuf[1]=line_i( "-2l", pbuf[2], pbuf[1], 0, 1, 1, 0 );
2173     lbuf[2]=line_i( "-3l", pbuf[1], pbuf[3], 0, 1, 1, 0 );
2174     lbuf[3]=line_i( "-4l", pbuf[3], pbuf[0], 0, 1, 1, 0 );
2175     sbuf=surface_i( "-1s", '+', -1, (int)4, "++++", lbuf, "llll");
2176     Sbuf=createBlendedNurbs(sbuf);
2177     //repNurs(Sbuf );
2178 
2179     /* delete the temp.surf,line */
2180     delPnt( 4, pbuf );
2181     delLine( 4, lbuf );
2182     delSurf( 1, &sbuf );
2183     return(Sbuf);
2184 }
2185 
2186 
surfToShape(int s)2187 int surfToShape(int s)
2188 {
2189   /* check if the surf is plane and generate a shape if yes */
2190 
2191   int pbuf[4], sbuf, i,j,n,l,c;
2192   double p1[3], p2[3], el[3], eh[3], eq[3], nr[3];
2193   double ptrans[3];
2194   double maxlhq[3]={-MAX_FLOAT,-MAX_FLOAT,-MAX_FLOAT}, minlhq[3]={MAX_FLOAT,MAX_FLOAT,MAX_FLOAT};
2195   double ex[3], ey[3], ez[3], vx[3]={1.,0.,0.}, vy[3]={0.,1.,0.}, vz[3]={0.,0.,1.};
2196   double p_xyz[4][3], p_lhq[4][3];
2197   char buffer[MAX_LINE_LENGTH];
2198 
2199   /* generate 3 points on the surface */
2200   if(surf[s].typ[0]=='l') l=surf[s].l[0];
2201   else l=lcmb[surf[s].l[0]].l[0];
2202   sem_wait(&sem_g);
2203   v_result( &point[line[l].p1].px, &point[line[l].p2].px, p1);
2204   sem_post(&sem_g);
2205   for(i=0; i<surf[s].nl; i++)
2206   {
2207     if(surf[s].typ[i]=='l')
2208     {
2209       l=surf[s].l[i];
2210    sem_wait(&sem_g);
2211      v_result( &point[line[l].p1].px, &point[line[l].p2].px, p2);
2212   sem_post(&sem_g);
2213       v_prod( p1, p2, nr );
2214       if (v_betrag(nr)>0.) break;
2215     }
2216     else
2217     {
2218       for(j=0;j<lcmb[surf[s].l[i]].nl; j++)
2219       {
2220         l=lcmb[surf[s].l[i]].l[j];
2221   sem_wait(&sem_g);
2222         v_result( &point[line[l].p1].px, &point[line[l].p2].px, p2);
2223   sem_post(&sem_g);
2224         v_prod( p1, p2, nr );
2225         if (v_betrag(nr)>0.) break;
2226       }
2227       if (v_betrag(nr)>0.) break;
2228     }
2229   }
2230   if (v_betrag(nr)==0.) { printf("ERROR in surfToShape, could not determine 3 independent points. Talk with the programmer.\n"); exit(0); }
2231   v_prod( nr, p1, p2 );
2232 
2233   /* check if the surface is plane (if the extention in eq is 0. ) */
2234   /* new coordinate system */
2235   v_norm( p1, el );
2236   v_norm( p2, eh );
2237   v_norm( nr, eq );
2238 
2239   /* transform the xyz unit-vectors into the lhq system */
2240   ex[0]=v_sprod(vx,el);
2241   ex[1]=v_sprod(vx,eh);
2242   ex[2]=v_sprod(vx,eq);
2243   ey[0]=v_sprod(vy,el);
2244   ey[1]=v_sprod(vy,eh);
2245   ey[2]=v_sprod(vy,eq);
2246   ez[0]=v_sprod(vz,el);
2247   ez[1]=v_sprod(vz,eh);
2248   ez[2]=v_sprod(vz,eq);
2249 
2250   /* transform all points of the surface into lhq coordinates */
2251   for(i=0; i<surf[s].nl; i++)
2252   {
2253     if(surf[s].typ[i]=='l')
2254     {
2255       l=surf[s].l[i];
2256       for (n=0; n<line[l].nip; n+=3)
2257       {
2258         for(j=0; j<3; j++)
2259         {
2260           ptrans[j]=line[l].ip[n]*ex[j] + line[l].ip[n+1]*ey[j] + line[l].ip[n+2]*ez[j];
2261           if(ptrans[j]>maxlhq[j]) maxlhq[j]=ptrans[j];
2262           if(ptrans[j]<minlhq[j]) minlhq[j]=ptrans[j];
2263 	  }
2264 	}
2265     }
2266     else
2267     {
2268       for (c=0; c<lcmb[surf[s].l[i]].nl; c++)
2269 	{
2270         l=lcmb[surf[s].l[i]].l[c];
2271         for (n=0; n<line[l].nip; n+=3)
2272         {
2273           for(j=0; j<3; j++)
2274           {
2275             ptrans[j]=line[l].ip[n]*ex[j] + line[l].ip[n+1]*ey[j] + line[l].ip[n+2]*ez[j];
2276             if(ptrans[j]>maxlhq[j]) maxlhq[j]=ptrans[j];
2277             if(ptrans[j]<minlhq[j]) minlhq[j]=ptrans[j];
2278 	    }
2279 	  }
2280       }
2281     }
2282   }
2283 
2284   /* check if the surf is plane: */
2285   if((maxlhq[2]-minlhq[2])>1.e-12) return(-1);
2286 
2287   //for(j=0; j<3; j++) printf("maxlhq:%lf minlhq:%lf\n", maxlhq[j],minlhq[j]);
2288 
2289   /* create points from maxlhq and minlhq which enclose the surface (p1 1st quadrant, pn: math+) */
2290   p_lhq[0][0]=maxlhq[0];
2291   p_lhq[0][1]=maxlhq[1];
2292   p_lhq[0][2]=maxlhq[2];
2293   p_lhq[1][0]=minlhq[0];
2294   p_lhq[1][1]=maxlhq[1];
2295   p_lhq[1][2]=maxlhq[2];
2296   p_lhq[2][0]=minlhq[0];
2297   p_lhq[2][1]=minlhq[1];
2298   p_lhq[2][2]=maxlhq[2];
2299 
2300   /* transform in xyz */
2301   for(i=0; i<3; i++) for(j=0; j<3; j++) p_xyz[i][j]=p_lhq[i][0]*el[j]+p_lhq[i][1]*eh[j]+p_lhq[i][2]*eq[j];
2302   //for(i=0; i<3; i++) for(j=0; j<3; j++) printf("p_xyz[%d]:%lf  %lf %lf %lf %lf %lf %lf \n", i, p_xyz[i][j], p_lhq[i][0],el[j],p_lhq[i][1],eh[j],p_lhq[i][2],eq[j]);
2303 
2304   sem_wait(&sem_g);
2305   for(i=0; i<3; i++)
2306   {
2307     getNewName( buffer, "p" );
2308     pbuf[i]=pnt( buffer , p_xyz[i][0], p_xyz[i][1], p_xyz[i][2], 0 );
2309   }
2310   getNewName( buffer, "sh" );
2311   sbuf=shape_i( buffer, 0, pbuf[0], pbuf[1], pbuf[2], 0, 0,0,0);
2312   sem_post(&sem_g);
2313 
2314   if(sbuf>-1) surf[s].sh=sbuf;
2315   printf("shape:%s %s %d\n", buffer, shape[sbuf].name, sbuf);
2316   return(sbuf);
2317 }
2318 
2319 
sphToNurs(int s,int flag)2320 int sphToNurs(int s, int flag)
2321 {
2322   // 170*3 u++(v++)
2323   double controlPoint[]={
2324    0.000000, 0.500120, 0.000000,
2325     0.141180, 0.500130, -0.102600,
2326     0.282390, 0.431590, -0.205200,
2327     0.398670, 0.294550, -0.289690,
2328     0.465240, 0.105030, -0.338090,
2329     0.465240, -0.104770, -0.338120,
2330     0.398670, -0.294260, -0.289690,
2331     0.282370, -0.431340, -0.205220,
2332     0.141230, -0.499890, -0.102610,
2333     -0.000010, -0.499910, -0.000010,
2334     0.000000, 0.500120, 0.000000,
2335     0.100910, 0.500130, -0.158010,
2336     0.201780, 0.431630, -0.316040,
2337     0.284880, 0.294540, -0.446230,
2338     0.332500, 0.105070, -0.520780,
2339     0.332500, -0.104740, -0.520810,
2340     0.284950, -0.294250, -0.446230,
2341     0.201840, -0.431300, -0.316120,
2342     0.100980, -0.499910, -0.158070,
2343     -0.000010, -0.499910, -0.000010,
2344     0.000000, 0.500120, 0.000000,
2345     0.035710, 0.500110, -0.195360,
2346     0.071440, 0.431610, -0.390730,
2347     0.100850, 0.294590, -0.551750,
2348     0.117730, 0.105070, -0.643910,
2349     0.117720, -0.104730, -0.643940,
2350     0.100860, -0.294230, -0.551740,
2351     0.071430, -0.431340, -0.390810,
2352     0.035690, -0.499870, -0.195390,
2353     -0.000010, -0.499910, -0.000010,
2354     0.000000, 0.500120, 0.000000,
2355     -0.044820, 0.500130, -0.201680,
2356     -0.089670, 0.431600, -0.403310,
2357     -0.126590, 0.294570, -0.569450,
2358     -0.147730, 0.105060, -0.664560,
2359     -0.147740, -0.104740, -0.664580,
2360     -0.126580, -0.294240, -0.569440,
2361     -0.089680, -0.431330, -0.403330,
2362     -0.044820, -0.499910, -0.201690,
2363     -0.000010, -0.499910, -0.000010,
2364     0.000000, 0.500120, 0.000000,
2365     -0.123360, 0.500120, -0.169650,
2366     -0.246620, 0.431590, -0.339300,
2367     -0.348150, 0.294550, -0.479080,
2368     -0.406320, 0.105040, -0.559160,
2369     -0.406330, -0.104740, -0.559130,
2370     -0.348130, -0.294290, -0.479130,
2371     -0.246570, -0.431340, -0.339380,
2372     -0.123300, -0.499920, -0.169720,
2373     -0.000010, -0.499910, -0.000010,
2374     0.000000, 0.500120, 0.000000,
2375     -0.178010, 0.500120, -0.104910,
2376     -0.355920, 0.431580, -0.209820,
2377     -0.502530, 0.294560, -0.296220,
2378     -0.586450, 0.105050, -0.345720,
2379     -0.586450, -0.104760, -0.345740,
2380     -0.502520, -0.294280, -0.296270,
2381     -0.355930, -0.431340, -0.209840,
2382     -0.177950, -0.499900, -0.104920,
2383     -0.000010, -0.499910, -0.000010,
2384     0.000000, 0.500120, 0.000000,
2385     -0.196880, 0.500100, -0.026330,
2386     -0.393730, 0.431560, -0.052740,
2387     -0.555920, 0.294550, -0.074460,
2388     -0.648810, 0.105020, -0.086880,
2389     -0.648750, -0.104770, -0.086910,
2390     -0.555910, -0.294290, -0.074510,
2391     -0.393680, -0.431350, -0.052760,
2392     -0.196830, -0.499890, -0.026420,
2393     -0.000010, -0.499910, -0.000010,
2394     0.000000, 0.500120, 0.000000,
2395     -0.181500, 0.500110, 0.047200,
2396     -0.362980, 0.431600, 0.094370,
2397     -0.512410, 0.294510, 0.133200,
2398     -0.597990, 0.105020, 0.155420,
2399     -0.597990, -0.104790, 0.155400,
2400     -0.512400, -0.294330, 0.133150,
2401     -0.362930, -0.431330, 0.094290,
2402     -0.181450, -0.499910, 0.047190,
2403     -0.000010, -0.499910, -0.000010,
2404     0.000000, 0.500120, 0.000000,
2405     -0.141240, 0.500100, 0.102600,
2406     -0.282380, 0.431550, 0.205210,
2407     -0.398690, 0.294500, 0.289740,
2408     -0.465240, 0.104990, 0.338110,
2409     -0.465250, -0.104820, 0.338090,
2410     -0.398670, -0.294340, 0.289690,
2411     -0.282340, -0.431360, 0.205190,
2412     -0.141180, -0.499920, 0.102600,
2413     -0.000010, -0.499910, -0.000010,
2414     0.000000, 0.500120, 0.000000,
2415     -0.100900, 0.500080, 0.158090,
2416     -0.201790, 0.431550, 0.316170,
2417     -0.284900, 0.294510, 0.446280,
2418     -0.332450, 0.105000, 0.520860,
2419     -0.332450, -0.104810, 0.520830,
2420     -0.284900, -0.294310, 0.446280,
2421     -0.201750, -0.431350, 0.316150,
2422     -0.100930, -0.499900, 0.158060,
2423     -0.000010, -0.499910, -0.000010,
2424     0.000000, 0.500120, 0.000000,
2425     -0.035700, 0.500110, 0.195440,
2426     -0.071390, 0.431600, 0.390860,
2427     -0.100820, 0.294510, 0.551850,
2428     -0.117680, 0.104990, 0.643990,
2429     -0.117680, -0.104820, 0.643960,
2430     -0.100810, -0.294330, 0.551800,
2431     -0.071390, -0.431350, 0.390780,
2432     -0.035650, -0.499910, 0.195430,
2433     -0.000010, -0.499910, -0.000010,
2434     0.000000, 0.500120, 0.000000,
2435     0.044830, 0.500090, 0.201770,
2436     0.089730, 0.431590, 0.403380,
2437     0.126620, 0.294500, 0.569490,
2438     0.147780, 0.105000, 0.664630,
2439     0.147780, -0.104810, 0.664610,
2440     0.126630, -0.294310, 0.569500,
2441     0.089710, -0.431340, 0.403360,
2442     0.044860, -0.499880, 0.201730,
2443     -0.000010, -0.499910, -0.000010,
2444     0.000000, 0.500120, 0.000000,
2445     0.123310, 0.500080, 0.169740,
2446     0.246610, 0.431590, 0.339430,
2447     0.348180, 0.294550, 0.479180,
2448     0.406380, 0.104990, 0.559180,
2449     0.406370, -0.104790, 0.559210,
2450     0.348190, -0.294290, 0.479130,
2451     0.246660, -0.431310, 0.339410,
2452     0.123340, -0.499890, 0.169700,
2453     -0.000010, -0.499910, -0.000010,
2454     0.000000, 0.500120, 0.000000,
2455     0.178020, 0.500100, 0.104990,
2456     0.355970, 0.431620, 0.209950,
2457     0.502560, 0.294530, 0.296320,
2458     0.586490, 0.105010, 0.345790,
2459     0.586490, -0.104790, 0.345770,
2460     0.502580, -0.294300, 0.296270,
2461     0.355960, -0.431330, 0.209870,
2462     0.178060, -0.499880, 0.104910,
2463     -0.000010, -0.499910, -0.000010,
2464     0.000000, 0.500120, 0.000000,
2465     0.196890, 0.500120, 0.026410,
2466     0.393730, 0.431610, 0.052810,
2467     0.555950, 0.294540, 0.074560,
2468     0.648800, 0.105020, 0.086960,
2469     0.648790, -0.104780, 0.086930,
2470     0.555970, -0.294300, 0.074510,
2471     0.393770, -0.431300, 0.052790,
2472     0.196940, -0.499900, 0.026410,
2473     -0.000010, -0.499910, -0.000010,
2474     0.000000, 0.500120, 0.000000,
2475     0.181510, 0.500110, -0.047110,
2476     0.362980, 0.431590, -0.094240,
2477     0.512440, 0.294580, -0.133100,
2478     0.598030, 0.105040, -0.155350,
2479     0.598030, -0.104760, -0.155370,
2480     0.512460, -0.294250, -0.133150,
2481     0.363020, -0.431320, -0.094260,
2482     0.181550, -0.499870, -0.047200,
2483     -0.000010, -0.499910, -0.000010,
2484     0.000000, 0.500120, 0.000000,
2485     0.141180, 0.500130, -0.102600,
2486     0.282390, 0.431590, -0.205200,
2487     0.398670, 0.294550, -0.289690,
2488     0.465240, 0.105030, -0.338090,
2489     0.465240, -0.104770, -0.338120,
2490     0.398670, -0.294260, -0.289690,
2491     0.282370, -0.431340, -0.205220,
2492     0.141230, -0.499890, -0.102610,
2493     -0.000010, -0.499910, -0.000010
2494   };
2495   // 26U, 20V
2496   double controlNod[]={
2497       0.000000
2498     , 0.000000
2499     , 0.000000
2500     , 0.000000
2501     , 0.000000
2502     , 0.000000
2503     , 0.000000
2504     , 0.000000
2505     , 0.000000
2506     , 1.000000
2507     , 1.000000
2508     , 1.000000
2509     , 1.000000
2510     , 1.000000
2511     , 1.000000
2512     , 1.000000
2513     , 1.000000
2514     , 2.000000
2515     , 2.000000
2516     , 2.000000
2517     , 2.000000
2518     , 2.000000
2519     , 2.000000
2520     , 2.000000
2521     , 2.000000
2522     , 2.000000
2523     , 0.000000
2524     , 0.000000
2525     , 0.000000
2526     , 0.000000
2527     , 0.000000
2528     , 0.000000
2529     , 0.000000
2530     , 0.000000
2531     , 0.000000
2532     , 0.000000
2533     , 1.000000
2534     , 1.000000
2535     , 1.000000
2536     , 1.000000
2537     , 1.000000
2538     , 1.000000
2539     , 1.000000
2540     , 1.000000
2541     , 1.000000
2542     , 1.000000 };
2543 
2544   int i,j,h,k,l,n,c;
2545   int nr=-1, pnr;
2546   char buffer[MAX_LINE_LENGTH];
2547 
2548   char name[MAX_LINE_LENGTH];
2549   int cp, pnts;
2550   double diameter, pbuf[3];
2551   double el[3], eh[3], eq[3];
2552   double p1[3], p2[3], nv[3], cg[3]={0,0,0};
2553 
2554   if(flag) h=surf[s].sh; else h=s;
2555 
2556   cp=shape[h].p[0];
2557   sem_wait(&sem_g);
2558   v_result( &point[shape[h].p[0]].px, &point[shape[h].p[1]].px, pbuf  );
2559   sem_post(&sem_g);
2560   diameter=v_betrag(pbuf)*2.;
2561 
2562 
2563   sem_wait(&sem_g);
2564 
2565   // use the shape pool to avoid conflicts with the master shape name
2566   nr= getNewName( name, "sh" );
2567   if ( nr == -1 )
2568     { printf(" ERROR: could not create new nurs\n"); return(-1); }
2569 
2570   // NURS ! DEFINE    8    9   17   10   26   20
2571 
2572   if ((nurbs = (Nurbs *)realloc( (Nurbs *)nurbs, (anzGeo->nurs+1)*sizeof(Nurbs)) ) == NULL )
2573   { printf("\n\nERROR: realloc failure in Nurs, nurbs:%s not installed\n\n", name); return(-1); }
2574 
2575   nr=anzGeo->nurs;
2576   hashNurs( sumAsci, name, nr );
2577   anzGeo->nurs++;
2578   i=strlen(name);
2579   if((nurbs[nr].name= (char *)malloc((i+1)*sizeof(char))) == NULL )
2580   { printf("ERROR: malloc failed\n\n" ); return(-1); }
2581   strcpy(nurbs[nr].name, name);
2582 
2583   /* save the original definition in a buffer */
2584   nurbs[nr].u_exp = 8;
2585   nurbs[nr].v_exp = 9;
2586   nurbs[nr].u_npnt= 17;
2587   nurbs[nr].v_npnt= 10;
2588   nurbs[nr].u_nknt= 26;
2589   nurbs[nr].v_nknt= 20;
2590   nurbs[nr].u_stride= 4* nurbs[nr].v_npnt;
2591   nurbs[nr].v_stride= 4;
2592   nurbs[nr].ctlarray=(GLfloat *)NULL;
2593 
2594   if ( (nurbs[nr].uknt = (GLfloat *)malloc( (nurbs[nr].u_nknt+1) * sizeof(GLfloat))) == NULL )
2595     printf("\n\n ERROR: realloc failed uknt\n\n");
2596   if ( (nurbs[nr].vknt = (GLfloat *)malloc( (nurbs[nr].v_nknt+1) * sizeof(GLfloat))) == NULL )
2597     printf("\n\n ERROR: realloc failed vknt\n\n");
2598 
2599   k=0;
2600   for(i=0; i<nurbs[nr].u_nknt; i++) { nurbs[nr].uknt[i]=controlNod[k++]; }
2601   for(i=0; i<nurbs[nr].v_nknt; i++) { nurbs[nr].vknt[i]=controlNod[k++]; }
2602 
2603   if ( (nurbs[nr].ctlpnt =
2604     (int **)malloc(  (nurbs[nr].u_npnt+1) * sizeof(int *))) == NULL )
2605     printf("\n\n ERROR: malloc failed ctlpnt\n\n");
2606   for (i=0; i<nurbs[nr].u_npnt; i++)
2607   {
2608     if ( (nurbs[nr].ctlpnt[i] =
2609       (int *)malloc(  (nurbs[nr].v_npnt+1) * sizeof( int ))) == NULL )
2610       printf("\n\n ERROR: malloc failed ctlpnt[i]\n\n");
2611   }
2612 
2613   if ( (nurbs[nr].weight =
2614     (GLfloat **)malloc(  (nurbs[nr].u_npnt+1) * sizeof(GLfloat *))) == NULL )
2615     printf("\n\n ERROR: malloc failed weight\n\n");
2616   for (i=0; i<nurbs[nr].u_npnt; i++)
2617   {
2618     if ( (nurbs[nr].weight[i] =
2619       (GLfloat *)malloc(  (nurbs[nr].v_npnt+1) * sizeof(GLfloat))) == NULL )
2620       printf("\n\n ERROR: malloc failed weight[i]\n\n");
2621   }
2622 
2623   k=0;
2624   for (i=0; i<nurbs[nr].u_npnt; i++)
2625   {
2626     for (j=0; j<nurbs[nr].v_npnt; j++)
2627     {
2628       getNewName( buffer, "p" );
2629       pnr= pnt( buffer, (controlPoint[k]*diameter), (controlPoint[k+1]*diameter), (controlPoint[k+2]*diameter), 0);
2630       k+=3;
2631       nurbs[nr].ctlpnt[i][j] = pnr;
2632       nurbs[nr].weight[i][j]=1.;
2633     }
2634   }
2635 
2636   /* additional variables for the trimming and meshing */
2637   nurbs[nr].nurbsType=0;
2638   nurbs[nr].trimFlag=0;
2639   nurbs[nr].patches=0;
2640 
2641   nurbs[nr].uv=NULL;
2642   nurbs[nr].xyz=NULL;
2643   nurbs[nr].np=NULL;
2644   nurbs[nr].nc=NULL;
2645   nurbs[nr].umax=NULL;
2646   nurbs[nr].vmax=NULL;
2647   nurbs[nr].ustep=NULL;
2648   nurbs[nr].vstep=NULL;
2649   nurbs[nr].sum_ambiguousPnts=NULL;
2650   nurbs[nr].uvflipped=NULL;
2651 
2652   nurbs[nr].endFlag=1;
2653   nurbs[nr].type=GL_MAP2_VERTEX_4;
2654 
2655   nurbs[nr].Nurb = (GLUnurbsObj *)gluNewNurbsRenderer();
2656 
2657   repNurs(nr);
2658 
2659   for (i=0; i<anz->sets; i++)
2660   {
2661     if ( set[i].flag=='o') seta( i, "S", nr );
2662   }
2663 
2664   sem_post(&sem_g);
2665 
2666   /* align the nurs with the shape */
2667   /* transform all points of the surf into lhq coordinates */
2668   /* generate lhq coordinates */
2669 
2670   /* vector to the cg of the surface */
2671   if(flag)
2672   {
2673     pnts=0;
2674     for(i=0; i<surf[s].nl; i++)
2675     {
2676       if(surf[s].typ[i]=='l')
2677       {
2678         l=surf[s].l[i];
2679         pnts+=line[l].nip;
2680         for (n=0; n<line[l].nip; n+=3)
2681         {
2682           for(j=0; j<3; j++)
2683           {
2684             cg[j]+=line[l].ip[n+j];
2685   	}
2686         }
2687       }
2688       else
2689       {
2690         for (c=0; c<lcmb[surf[s].l[i]].nl; c++)
2691         {
2692           l=lcmb[surf[s].l[i]].l[c];
2693           pnts+=line[l].nip;
2694           for (n=0; n<line[l].nip; n+=3)
2695           {
2696             for(j=0; j<3; j++)
2697             {
2698               cg[j]+=line[l].ip[n+j];
2699   	  }
2700           }
2701         }
2702       }
2703     }
2704     pnts/=3;
2705     for(j=0; j<3; j++) cg[j]/=pnts;
2706 
2707     /* vector to u/2,v/2 */
2708     sem_wait(&sem_g);
2709     pnr=nurbs[nr].ctlpnt[6][5];
2710     v_result( &point[shape[h].p[0]].px, &point[pnr].px, p1);
2711 
2712     /* vector from origin to cg surface */
2713     v_result( &point[shape[h].p[0]].px, cg, p2);
2714     sem_post(&sem_g);
2715   }
2716   else
2717   {
2718     sem_wait(&sem_g);
2719     v_result( &point[shape[h].p[0]].px, &point[shape[h].p[1]].px, p1);
2720     v_result( &point[shape[h].p[0]].px, &point[shape[h].p[2]].px, p2);
2721     sem_post(&sem_g);
2722   }
2723 
2724 #if TEST1
2725   v_norm( p1, p1 );
2726   for(j=0; j<3; j++)  printf("p:%s v sph xyz:%f\n",point[pnr].name,p1[j]);
2727   v_norm( p2, p2 );
2728   for(j=0; j<3; j++)  printf(" v surface xyz:%f\n",p2[j]);
2729 #endif
2730 
2731   /* generate 2 perpendicular vectors in this shape */
2732   /* p2==eq must point in the direction of cg surf */
2733   v_prod( p1, p2, nv );
2734   v_prod( p2, nv, p1 );
2735 
2736   v_norm( nv, el );
2737   v_norm( p1, eh );
2738   v_norm( p2, eq );
2739 #if TEST1
2740   for(j=0; j<3; j++)  printf("lhq xyz:%f %f %f\n",el[j],eh[j],eq[j]);
2741 #endif
2742 
2743   /* transform in xyz */
2744   sem_wait(&sem_g);
2745   for (i=0; i<nurbs[nr].u_npnt; i++)
2746   {
2747     for (j=0; j<nurbs[nr].v_npnt; j++)
2748     {
2749       pbuf[0]=point[nurbs[nr].ctlpnt[i][j]].px;
2750       pbuf[1]=point[nurbs[nr].ctlpnt[i][j]].py;
2751       pbuf[2]=point[nurbs[nr].ctlpnt[i][j]].pz;
2752       point[nurbs[nr].ctlpnt[i][j]].px= pbuf[0] *el[0] + pbuf[1] * eh[0] + pbuf[2] * eq[0] + point[cp].px;
2753       point[nurbs[nr].ctlpnt[i][j]].py= pbuf[0] *el[1] + pbuf[1] * eh[1] + pbuf[2] * eq[1] + point[cp].py;
2754       point[nurbs[nr].ctlpnt[i][j]].pz= pbuf[0] *el[2] + pbuf[1] * eh[2] + pbuf[2] * eq[2] + point[cp].pz;
2755     }
2756   }
2757 #if TEST1
2758   v_result( &point[shape[h].p[0]].px, &point[pnr].px, p1);
2759   v_norm( p1, p1 );
2760   for(j=0; j<3; j++)  printf("p:%s v sph xyz:%f\n",point[pnr].name,p1[j]);
2761 #endif
2762   repNurs(nr);
2763   sem_post(&sem_g);
2764 
2765   return(nr); // Nurbsindex
2766 }
2767 
2768 
torusToNurs(int s,int flag)2769 int torusToNurs(int s, int flag)
2770 {
2771   // 81*4 u++(v++)
2772   double controlPoint[]={
2773 0.000000, 3.000000, 0.000000, 1.000000  ,
2774 -1.000000, 3.000000, 0.000000, 0.707107	,
2775 -1.000000, 2.000000, 0.000000, 1.000000	,
2776 -1.000000, 1.000000, 0.000000, 0.707107	,
2777 0.000000, 1.000000, 0.000000, 1.000000 	,
2778 1.000000, 1.000000, 0.000000, 0.707107 	,
2779 1.000000, 2.000000, 0.000000, 1.000000 	,
2780 1.000000, 3.000000, 0.000000, 0.707107 	,
2781 0.000000, 3.000000, 0.000000, 1.000000 	,
2782 0.000000, 3.000000, 3.000000, 0.707107 	,
2783 -1.000000, 3.000000, 3.000000, 0.500000	,
2784 -1.000000, 2.000000, 2.000000, 0.707107	,
2785 -1.000000, 1.000000, 1.000000, 0.500000	,
2786 0.000000, 1.000000, 1.000000, 0.707107 	,
2787 1.000000, 1.000000, 1.000000, 0.500000 	,
2788 1.000000, 2.000000, 2.000000, 0.707107 	,
2789 1.000000, 3.000000, 3.000000, 0.500000 	,
2790 0.000000, 3.000000, 3.000000, 0.707107 	,
2791 0.000000, 0.000000, 3.000000, 1.000000 	,
2792 -1.000000, 0.000000, 3.000000, 0.707107	,
2793 -1.000000, 0.000000, 2.000000, 1.000000	,
2794 -1.000000, 0.000000, 1.000000, 0.707107	,
2795 0.000000, 0.000000, 1.000000, 1.000000 	,
2796 1.000000, 0.000000, 1.000000, 0.707107 	,
2797 1.000000, 0.000000, 2.000000, 1.000000 	,
2798 1.000000, 0.000000, 3.000000, 0.707107 	,
2799 0.000000, 0.000000, 3.000000, 1.000000 	,
2800 0.000000, -3.000000, 3.000000, 0.707107	,
2801 -1.000000, -3.000000, 3.000000, 0.500000,
2802 -1.000000, -2.000000, 2.000000, 0.707107,
2803 -1.000000, -1.000000, 1.000000, 0.500000,
2804 0.000000, -1.000000, 1.000000, 0.707107 ,
2805 1.000000, -1.000000, 1.000000, 0.500000 ,
2806 1.000000, -2.000000, 2.000000, 0.707107 ,
2807 1.000000, -3.000000, 3.000000, 0.500000 ,
2808 0.000000, -3.000000, 3.000000, 0.707107 ,
2809 0.000000, -3.000000, 0.000000, 1.000000 ,
2810 -1.000000, -3.000000, 0.000000, 0.707107,
2811 -1.000000, -2.000000, 0.000000, 1.000000,
2812 -1.000000, -1.000000, 0.000000, 0.707107,
2813 0.000000, -1.000000, 0.000000, 1.000000 ,
2814 1.000000, -1.000000, 0.000000, 0.707107 ,
2815 1.000000, -2.000000, 0.000000, 1.000000 ,
2816 1.000000, -3.000000, 0.000000, 0.707107 ,
2817 0.000000, -3.000000, 0.000000, 1.000000 ,
2818 0.000000, -3.000000, -3.000000, 0.707107,
2819 -1.000000, -3.000000, -3.000000, 0.500000,
2820 -1.000000, -2.000000, -2.000000, 0.707107,
2821 -1.000000, -1.000000, -1.000000, 0.500000,
2822 0.000000, -1.000000, -1.000000, 0.707107,
2823 1.000000, -1.000000, -1.000000, 0.500000,
2824 1.000000, -2.000000, -2.000000, 0.707107,
2825 1.000000, -3.000000, -3.000000, 0.500000,
2826 0.000000, -3.000000, -3.000000, 0.707107,
2827 0.000000, 0.000000, -3.000000, 1.000000 ,
2828 -1.000000, 0.000000, -3.000000, 0.707107,
2829 -1.000000, 0.000000, -2.000000, 1.000000,
2830 -1.000000, 0.000000, -1.000000, 0.707107,
2831 0.000000, 0.000000, -1.000000, 1.000000 ,
2832 1.000000, 0.000000, -1.000000, 0.707107 ,
2833 1.000000, 0.000000, -2.000000, 1.000000 ,
2834 1.000000, 0.000000, -3.000000, 0.707107 ,
2835 0.000000, 0.000000, -3.000000, 1.000000 ,
2836 0.000000, 3.000000, -3.000000, 0.707107 ,
2837 -1.000000, 3.000000, -3.000000, 0.500000,
2838 -1.000000, 2.000000, -2.000000, 0.707107,
2839 -1.000000, 1.000000, -1.000000, 0.500000,
2840 0.000000, 1.000000, -1.000000, 0.707107 ,
2841 1.000000, 1.000000, -1.000000, 0.500000 ,
2842 1.000000, 2.000000, -2.000000, 0.707107 ,
2843 1.000000, 3.000000, -3.000000, 0.500000 ,
2844 0.000000, 3.000000, -3.000000, 0.707107 ,
2845 0.000000, 3.000000, 0.000000, 1.000000  ,
2846 -1.000000, 3.000000, 0.000000, 0.707107 ,
2847 -1.000000, 2.000000, 0.000000, 1.000000 ,
2848 -1.000000, 1.000000, 0.000000, 0.707107 ,
2849 0.000000, 1.000000, 0.000000, 1.000000  ,
2850 1.000000, 1.000000, 0.000000, 0.707107	,
2851 1.000000, 2.000000, 0.000000, 1.000000	,
2852 1.000000, 3.000000, 0.000000, 0.707107	,
2853 0.000000, 3.000000, 0.000000, 1.000000  ,
2854   };
2855   // 12U, 12V
2856   double controlNod[]={
2857     0.000000,
2858     0.000000,
2859     0.000000,
2860     0.250000,
2861     0.250000,
2862     0.500000,
2863     0.500000,
2864     0.750000,
2865     0.750000,
2866     1.000000,
2867     1.000000,
2868     1.000000,
2869     0.000000,
2870     0.000000,
2871     0.000000,
2872     0.250000,
2873     0.250000,
2874     0.500000,
2875     0.500000,
2876     0.750000,
2877     0.750000,
2878     1.000000,
2879     1.000000,
2880     1.000000,
2881   };
2882 
2883 
2884   int i,j,h,k,kbuf,l,n,c;
2885   int nr=-1, pnr;
2886   char buffer[MAX_LINE_LENGTH];
2887 
2888   char name[MAX_LINE_LENGTH];
2889   int cp,pnts, *vpnts;
2890   double scaleFac1,scaleFac2, pbuf[3];
2891 
2892   double master_radius1=2.;
2893   double master_radius2=1.;
2894 
2895   double el[3], eh[3], eq[3];
2896   double ex[3], ey[3], ez[3], vx[3]={1.,0.,0.}, vy[3]={0.,1.,0.}, vz[3]={0.,0.,1.};
2897   double p1[3], p2[3], nv[3], cg[3]={0,0,0}, cgs[3];
2898   double **cgt, vcgtcgs[3],vcgtpv0[3],vcgtpv1[3],fi;
2899   int nurbsbuf[2];
2900 
2901   if(flag) h=surf[s].sh; else h=s;
2902 
2903   cp=shape[h].p[0];
2904   sem_wait(&sem_g);
2905   v_result( &point[shape[h].p[0]].px, &point[shape[h].p[2]].px, pbuf  );
2906   sem_post(&sem_g);
2907   scaleFac1=v_betrag(pbuf) / master_radius1;
2908   sem_wait(&sem_g);
2909   v_result( &point[shape[h].p[2]].px, &point[shape[h].p[3]].px, pbuf  );
2910   sem_post(&sem_g);
2911   scaleFac2=v_betrag(pbuf) / master_radius2 /scaleFac1;
2912 
2913   //printf("r1:%f r2:%f\n", scaleFac1,scaleFac2);
2914 
2915   sem_wait(&sem_g);
2916 
2917   //strcpy(name,shape[h].name);
2918   //nr= getNewName( name, "S" );
2919   // use the shape pool to avoid conflicts with the master shape name
2920   nr= getNewName( name, "sh" );
2921   if ( nr == -1 )
2922     { printf(" ERROR: could not create new nurs\n"); return(-1); }
2923 
2924 
2925   if ((nurbs = (Nurbs *)realloc( (Nurbs *)nurbs, (anzGeo->nurs+1)*sizeof(Nurbs)) ) == NULL )
2926   { printf("\n\nERROR: realloc failure in Nurs, nurbs:%s not installed\n\n", name); return(-1); }
2927 
2928   nr=anzGeo->nurs;
2929   hashNurs( sumAsci, name, nr );
2930   anzGeo->nurs++;
2931   i=strlen(name);
2932   if((nurbs[nr].name= (char *)malloc((i+1)*sizeof(char))) == NULL )
2933   { printf("ERROR: malloc failed\n\n" ); return(-1); }
2934 
2935   strcpy(nurbs[nr].name, name);
2936 
2937   /* save the original definition in a buffer */
2938   nurbs[nr].u_exp = 2;
2939   nurbs[nr].v_exp = 2;
2940   nurbs[nr].u_npnt= 9;
2941   nurbs[nr].v_npnt= 9;
2942   nurbs[nr].u_nknt= 12;
2943   nurbs[nr].v_nknt= 12;
2944   nurbs[nr].u_stride= 4* nurbs[nr].v_npnt;
2945   nurbs[nr].v_stride= 4;
2946   nurbs[nr].ctlarray=(GLfloat *)NULL;
2947 
2948   if ( (nurbs[nr].uknt = (GLfloat *)malloc( (nurbs[nr].u_nknt+1) * sizeof(GLfloat))) == NULL )
2949     printf("\n\n ERROR: realloc failed uknt\n\n");
2950   if ( (nurbs[nr].vknt = (GLfloat *)malloc( (nurbs[nr].v_nknt+1) * sizeof(GLfloat))) == NULL )
2951     printf("\n\n ERROR: realloc failed vknt\n\n");
2952 
2953   k=0;
2954   for(i=0; i<nurbs[nr].u_nknt; i++) { nurbs[nr].uknt[i]=controlNod[k++]; }
2955   for(i=0; i<nurbs[nr].v_nknt; i++) { nurbs[nr].vknt[i]=controlNod[k++]; }
2956 
2957   if ( (nurbs[nr].ctlpnt =
2958     (int **)malloc(  (nurbs[nr].u_npnt+1) * sizeof(int *))) == NULL )
2959     printf("\n\n ERROR: malloc failed ctlpnt\n\n");
2960   for (i=0; i<nurbs[nr].u_npnt; i++)
2961   {
2962     if ( (nurbs[nr].ctlpnt[i] =
2963       (int *)malloc(  (nurbs[nr].v_npnt+1) * sizeof( int ))) == NULL )
2964       printf("\n\n ERROR: malloc failed ctlpnt[i]\n\n");
2965   }
2966 
2967   if ( (nurbs[nr].weight =
2968     (GLfloat **)malloc(  (nurbs[nr].u_npnt+1) * sizeof(GLfloat *))) == NULL )
2969     printf("\n\n ERROR: malloc failed weight\n\n");
2970   for (i=0; i<nurbs[nr].u_npnt; i++)
2971   {
2972     if ( (nurbs[nr].weight[i] =
2973       (GLfloat *)malloc(  (nurbs[nr].v_npnt+1) * sizeof(GLfloat))) == NULL )
2974       printf("\n\n ERROR: malloc failed weight[i]\n\n");
2975   }
2976 
2977   if ( (cgt= (double **)malloc(  (nurbs[nr].u_npnt+1) * sizeof(double *))) == NULL )
2978     printf("\n\n ERROR: malloc failed\n\n");
2979   for (i=0; i<nurbs[nr].u_npnt; i++)
2980   {
2981     if ( (cgt[i]= (double *)malloc(  (3) * sizeof(double))) == NULL )
2982     printf("\n\n ERROR: malloc failed\n\n");
2983   }
2984 
2985   nurbsbuf[0]=nurbs[nr].u_npnt;
2986   nurbsbuf[1]=nurbs[nr].v_npnt;
2987   sem_post(&sem_g);
2988   k=0;
2989   for (i=0; i<nurbsbuf[0]; i++)
2990   {
2991     /* scale the points of the tube relative to the local center */
2992     /* determine the center, u==0 has y=0 u==1 -> y+ */
2993     cgt[i][0]=0.;
2994     cgt[i][1]=0.;
2995     cgt[i][2]=0.;
2996     kbuf=k;
2997     for (j=1; j<nurbsbuf[1]; j++)
2998     {
2999       cgt[i][1]+=controlPoint[k+1];
3000       cgt[i][2]+=controlPoint[k+2];
3001       k+=4;
3002     }
3003     j--;
3004     cgt[i][1]/=j;
3005     cgt[i][2]/=j;
3006 #if TEST1
3007     printf("cg torus section %f %f %f scal1 %f %f\n", cgt[i][0], cgt[i][1], cgt[i][2],scaleFac1,scaleFac2);
3008 #endif
3009     k=kbuf;
3010     for (j=0; j<nurbsbuf[1]; j++)
3011     {
3012       /* all points of that u have to be scaled by 'radius' */
3013       controlPoint[k]=(controlPoint[k]-cgt[i][0])*scaleFac2 + cgt[i][0];
3014       controlPoint[k+1]=(controlPoint[k+1]-cgt[i][1])*scaleFac2 + cgt[i][1];
3015       controlPoint[k+2]=(controlPoint[k+2]-cgt[i][2])*scaleFac2 + cgt[i][2];
3016 
3017       /* scale the torus to the radius1 */
3018       controlPoint[k]=controlPoint[k]*scaleFac1;
3019       controlPoint[k+1]=controlPoint[k+1]*scaleFac1;
3020       controlPoint[k+2]=controlPoint[k+2]*scaleFac1;
3021 
3022       /* generate the nurbs controll point */
3023       // the last point is the first point
3024     sem_wait(&sem_g);
3025       if(j==nurbs[nr].v_npnt-1)
3026       {
3027         nurbs[nr].ctlpnt[i][j] = nurbs[nr].ctlpnt[i][0];
3028       }
3029       else
3030       {
3031         getNewName( buffer, "p" );
3032         pnr= pnt( buffer, (controlPoint[k]), (controlPoint[k+1]), (controlPoint[k+2]), 0);
3033         nurbs[nr].ctlpnt[i][j] = pnr;
3034       }
3035       nurbs[nr].weight[i][j]= controlPoint[k+3];
3036     sem_post(&sem_g);
3037       k+=4;
3038     }
3039     /* scale cgt */
3040     cgt[i][1]*=scaleFac1;
3041     cgt[i][2]*=scaleFac1;
3042   }
3043 
3044   /* additional variables for the trimming and meshing */
3045   sem_wait(&sem_g);
3046   nurbs[nr].nurbsType=0;
3047   nurbs[nr].trimFlag=0;
3048   nurbs[nr].patches=0;
3049 
3050   nurbs[nr].uv=NULL;
3051   nurbs[nr].xyz=NULL;
3052   nurbs[nr].np=NULL;
3053   nurbs[nr].nc=NULL;
3054   nurbs[nr].umax=NULL;
3055   nurbs[nr].vmax=NULL;
3056   nurbs[nr].ustep=NULL;
3057   nurbs[nr].vstep=NULL;
3058   nurbs[nr].sum_ambiguousPnts=NULL;
3059   nurbs[nr].uvflipped=NULL;
3060 
3061   nurbs[nr].endFlag=1;
3062   nurbs[nr].type=GL_MAP2_VERTEX_4;
3063 
3064   nurbs[nr].Nurb = (GLUnurbsObj *)gluNewNurbsRenderer();
3065 
3066   for (i=0; i<anz->sets; i++)
3067   {
3068     if ( set[i].flag=='o') seta( i, "S", nr );
3069   }
3070 
3071   /* align the nurs with the shape */
3072 
3073   /* transform all points of the nurs into lhq coordinates */
3074   /* generate lhq coordinates */
3075   /* axis vector */
3076   v_result( &point[shape[h].p[0]].px, &point[shape[h].p[1]].px, p1);
3077   sem_post(&sem_g);
3078 
3079   /* vector to the cg of the surface */
3080   if(flag)
3081   {
3082     pnts=0;
3083     for(i=0; i<surf[s].nl; i++)
3084     {
3085       if(surf[s].typ[i]=='l')
3086       {
3087         l=surf[s].l[i];
3088         pnts+=line[l].nip;
3089         for (n=0; n<line[l].nip; n+=3)
3090         {
3091           for(j=0; j<3; j++)
3092           {
3093             cg[j]+=line[l].ip[n+j];
3094   	}
3095         }
3096       }
3097       else
3098       {
3099         for (c=0; c<lcmb[surf[s].l[i]].nl; c++)
3100         {
3101           l=lcmb[surf[s].l[i]].l[c];
3102           pnts+=line[l].nip;
3103           for (n=0; n<line[l].nip; n+=3)
3104           {
3105             for(j=0; j<3; j++)
3106             {
3107               cg[j]+=line[l].ip[n+j];
3108   	  }
3109           }
3110         }
3111       }
3112     }
3113     pnts/=3;
3114     for(j=0; j<3; j++) cg[j]/=pnts;
3115 
3116     /* vector from torus origin to cg surface */
3117     sem_wait(&sem_g);
3118     v_result( &point[shape[h].p[0]].px, cg, p2);
3119     sem_post(&sem_g);
3120   }
3121   else
3122   {
3123     sem_wait(&sem_g);
3124     v_result( &point[shape[h].p[0]].px, &point[shape[h].p[2]].px, p2);
3125     sem_post(&sem_g);
3126   }
3127 
3128   /* generate 2 perpendicular vectors in this shape */
3129   /* orig
3130   v_prod( p1, p2, nv );
3131   v_prod( nv, p1, p2 );
3132 
3133   v_norm( p1, el );
3134   v_norm( p2, eh );
3135   v_norm( nv, eq );
3136   */
3137   /* p2==eq must point in the opposite direction of cg surf */
3138   v_prod( p1, p2, nv );
3139   v_prod( p1, nv, p2 );
3140 
3141   v_norm( p1, el );
3142   v_norm( nv, eh );
3143   v_norm( p2, eq );
3144 
3145   /* the tube of the torus must be rotated around its axis to keep the surf on one side */
3146   /* transform the xyz unit-vectors into the lhq system */
3147   ex[0]=v_sprod(vx,el);
3148   ex[1]=v_sprod(vx,eh);
3149   ex[2]=v_sprod(vx,eq);
3150   ey[0]=v_sprod(vy,el);
3151   ey[1]=v_sprod(vy,eh);
3152   ey[2]=v_sprod(vy,eq);
3153   ez[0]=v_sprod(vz,el);
3154   ez[1]=v_sprod(vz,eh);
3155   ez[2]=v_sprod(vz,eq);
3156   /* transform cg into lhq coordinates */
3157   sem_wait(&sem_g);
3158   for(j=0; j<3; j++)
3159   {
3160     cgs[j]=(cg[0]-point[shape[h].p[0]].px)*ex[j] + (cg[1]-point[shape[h].p[0]].py)*ey[j] + (cg[2]-point[shape[h].p[0]].pz)*ez[j];
3161 #if TEST1
3162     printf(" cg surface lhq:%f xyz:%f\n",cgs[j],cg[j]);
3163 #endif
3164   }
3165   sem_post(&sem_g);
3166 
3167   /* determine the rotation angle around the center of the tube */
3168   /* determine the vector from cgt[] to cgs */
3169   i=6;
3170   v_result(cgt[i],cgs,vcgtcgs);
3171   /* determine the vector from cgt[] to torus point at u==0 */
3172 
3173   sem_wait(&sem_g);
3174 #if TEST1
3175   pnr=nurbs[nr].ctlpnt[i][2];
3176   printf(" nurbs_ctrp(%d,2):%s lhq:%f %f %f\n",i,point[pnr].name,point[pnr].px,point[pnr].py,point[pnr].pz);
3177   pnr=nurbs[nr].ctlpnt[i][1];
3178   printf(" nurbs_ctrp(%d,1):%s lhq:%f %f %f\n",i,point[pnr].name,point[pnr].px,point[pnr].py,point[pnr].pz);
3179 #endif
3180   pnr=nurbs[nr].ctlpnt[i][0];
3181 #if TEST1
3182   printf(" nurbs_ctrp(%d,0):%s lhq:%f %f %f\n",i,point[pnr].name,point[pnr].px,point[pnr].py,point[pnr].pz);
3183   for(j=0; j<3; j++) printf(" cgt lhq:%f\n",cgt[i][j]);
3184 #endif
3185   v_result(cgt[i],&point[pnr].px,vcgtpv0);
3186   /* determine the rotational axis */
3187   pnr=nurbs[nr].ctlpnt[i][1];
3188   v_result(cgt[i],&point[pnr].px,vcgtpv1);
3189   sem_post(&sem_g);
3190   v_prod(vcgtpv0,vcgtpv1,nv);
3191   /* angle */
3192   fi=v_angle_ref(vcgtpv0,vcgtcgs,nv);
3193 #if TEST1
3194   printf(" angle from surf to torus start deg:%f\n",fi*180./PI);
3195 #endif
3196   /* since pv0 should be located opposite to cgs substract 180 deg */
3197   fi-=PI;
3198   /* only n*PI is allowed */
3199   if(fi<0.) fi+=2*PI;
3200 #if TEST1
3201   printf(" deg:%f\n",fi*180./PI);
3202 #endif
3203 
3204   /* rotate the points at all u */
3205   if(fi<PI*.25) k=1;
3206   else if(fi<PI*.75) k=3;
3207   else if(fi<PI*1.25) k=5;
3208   else if(fi<PI*1.75) k=7;
3209   else k=1;
3210   if(k>1)
3211   {
3212   sem_wait(&sem_g);
3213    if ( (vpnts = (int *)malloc(  (nurbs[nr].v_npnt+1) * sizeof(int))) == NULL )
3214      printf("\n\n ERROR: malloc failed vpnts\n\n");
3215 
3216    for (i=0; i<nurbs[nr].u_npnt; i++)
3217    {
3218     for (j=0; j<nurbs[nr].v_npnt-k; j++)
3219     {
3220       vpnts[j]=nurbs[nr].ctlpnt[i][j+k-1];
3221     }
3222     n=0;
3223     for (j=nurbs[nr].v_npnt-k; j<nurbs[nr].v_npnt-1; j++)
3224     {
3225       vpnts[j]=nurbs[nr].ctlpnt[i][n++];
3226     }
3227     vpnts[j]=vpnts[0];
3228     for (j=0; j<nurbs[nr].v_npnt; j++) nurbs[nr].ctlpnt[i][j]=vpnts[j];
3229     //for (j=0; j<nurbs[nr].v_npnt; j++) printf(" nurbs_ctrp(%d,%d):%s\n",i,j,point[nurbs[nr].ctlpnt[i][j]].name);
3230    }
3231   sem_post(&sem_g);
3232    free(vpnts);
3233   }
3234 
3235   /* transform the torus in xyz */
3236   sem_wait(&sem_g);
3237   for (i=0; i<nurbs[nr].u_npnt; i++)
3238   {
3239     for (j=0; j<nurbs[nr].v_npnt-1; j++)
3240     {
3241       pbuf[0]=point[nurbs[nr].ctlpnt[i][j]].px;
3242       pbuf[1]=point[nurbs[nr].ctlpnt[i][j]].py;
3243       pbuf[2]=point[nurbs[nr].ctlpnt[i][j]].pz;
3244       point[nurbs[nr].ctlpnt[i][j]].px= pbuf[0] *el[0] + pbuf[1] * eh[0] + pbuf[2] * eq[0] + point[cp].px;
3245       point[nurbs[nr].ctlpnt[i][j]].py= pbuf[0] *el[1] + pbuf[1] * eh[1] + pbuf[2] * eq[1] + point[cp].py;
3246       point[nurbs[nr].ctlpnt[i][j]].pz= pbuf[0] *el[2] + pbuf[1] * eh[2] + pbuf[2] * eq[2] + point[cp].pz;
3247     }
3248   }
3249   repNurs(nr);
3250 
3251   for (i=0; i<nurbs[nr].u_npnt; i++) free(cgt[i]);
3252   sem_post(&sem_g);
3253   free(cgt);
3254 
3255   return(nr); // Nurbsindex
3256 }
3257 
3258 
3259 
coneToNurs(int s,int flag)3260 int coneToNurs(int s, int flag)
3261 {
3262   int pbuf[9][2], i, j, h, n, l, k, c, pnts, S;
3263   double p1[3], p2[3], cg[3]={0,0,0}, el[3], eh[3], eq[3], nr[3];
3264   double ptrans[3], dl,dr,qdrdl, dr1,dr2;
3265   //double maxlhq[3]={-MAX_FLOAT,-MAX_FLOAT,-MAX_FLOAT}, minlhq[3]={MAX_FLOAT,MAX_FLOAT,MAX_FLOAT};
3266   double maxlhq[3]={-2,-2,-2}, minlhq[3]={2,2,2};
3267   double ex[3], ey[3], ez[3], vx[3]={1.,0.,0.}, vy[3]={0.,1.,0.}, vz[3]={0.,0.,1.};
3268   double p_xyz[16][3], p_lhq[16][3];
3269   char buffer[MAX_LINE_LENGTH];
3270   double rad1, rad2;
3271 
3272   if(flag) h=surf[s].sh; else h=s;
3273 
3274   sem_wait(&sem_g);
3275   v_result( &point[shape[h].p[0]].px, &point[shape[h].p[2]].px, p1  );
3276   sem_post(&sem_g);
3277   rad1=v_betrag(p1);
3278   if(shape[h].type==2)
3279   {
3280   sem_wait(&sem_g);
3281     v_result( &point[shape[h].p[1]].px, &point[shape[h].p[3]].px, p2  );
3282   sem_post(&sem_g);
3283     rad2=v_betrag(p2);
3284   }
3285   else rad2=rad1;
3286   //printf("r1,2 %f %f\n",rad1,rad2);
3287 
3288   /* generate lhq coordinates */
3289   /* axis vector */
3290   sem_wait(&sem_g);
3291   v_result( &point[shape[h].p[0]].px, &point[shape[h].p[1]].px, p1);
3292   sem_post(&sem_g);
3293 
3294   /* vector to the cg of the surface */
3295   if(flag)
3296   {
3297     pnts=0;
3298     for(i=0; i<surf[s].nl; i++)
3299     {
3300       if(surf[s].typ[i]=='l')
3301       {
3302         l=surf[s].l[i];
3303         pnts+=line[l].nip;
3304         for (n=0; n<line[l].nip; n+=3)
3305         {
3306           for(j=0; j<3; j++)
3307           {
3308             cg[j]+=line[l].ip[n+j];
3309   	  }
3310         }
3311       }
3312       else
3313       {
3314         for (c=0; c<lcmb[surf[s].l[i]].nl; c++)
3315         {
3316           l=lcmb[surf[s].l[i]].l[c];
3317           pnts+=line[l].nip;
3318           for (n=0; n<line[l].nip; n+=3)
3319           {
3320             for(j=0; j<3; j++)
3321             {
3322               cg[j]+=line[l].ip[n+j];
3323   	  }
3324           }
3325         }
3326       }
3327     }
3328     pnts/=3;
3329     for(j=0; j<3; j++) cg[j]/=pnts;
3330     sem_wait(&sem_g);
3331     v_result( &point[shape[h].p[0]].px, cg, p2);
3332     sem_post(&sem_g);
3333   }
3334   else
3335   {
3336     sem_wait(&sem_g);
3337     v_result( &point[shape[h].p[0]].px, &point[shape[h].p[2]].px, p2);
3338     sem_post(&sem_g);
3339   }
3340 
3341   /* generate 2 perpendicular vectors in this shape */
3342   v_prod( p1, p2, nr );
3343   v_prod( nr, p1, p2 );
3344 
3345   v_norm( p1, el );
3346   v_norm( p2, eh );
3347   v_norm( nr, eq );
3348 
3349   /* transform the xyz unit-vectors into the lhq system */
3350   ex[0]=v_sprod(vx,el);
3351   ex[1]=v_sprod(vx,eh);
3352   ex[2]=v_sprod(vx,eq);
3353   ey[0]=v_sprod(vy,el);
3354   ey[1]=v_sprod(vy,eh);
3355   ey[2]=v_sprod(vy,eq);
3356   ez[0]=v_sprod(vz,el);
3357   ez[1]=v_sprod(vz,eh);
3358   ez[2]=v_sprod(vz,eq);
3359 
3360   /* transform all points of the surface into lhq coordinates */
3361   if(flag)
3362   {
3363     for(i=0; i<surf[s].nl; i++)
3364     {
3365       if(surf[s].typ[i]=='l')
3366       {
3367         l=surf[s].l[i];
3368         for (n=0; n<line[l].nip; n+=3)
3369         {
3370           for(j=0; j<3; j++)
3371           {
3372     sem_wait(&sem_g);
3373             ptrans[j]=(line[l].ip[n]-point[shape[h].p[0]].px)*ex[j] + (line[l].ip[n+1]-point[shape[h].p[0]].py)*ey[j] + (line[l].ip[n+2]-point[shape[h].p[0]].pz)*ez[j];
3374     sem_post(&sem_g);
3375             if(ptrans[j]>maxlhq[j]) maxlhq[j]=ptrans[j];
3376             if(ptrans[j]<minlhq[j]) minlhq[j]=ptrans[j];
3377           }
3378         }
3379       }
3380       else
3381       {
3382         for (c=0; c<lcmb[surf[s].l[i]].nl; c++)
3383         {
3384           l=lcmb[surf[s].l[i]].l[c];
3385           for (n=0; n<line[l].nip; n+=3)
3386           {
3387             for(j=0; j<3; j++)
3388             {
3389     sem_wait(&sem_g);
3390               ptrans[j]=(line[l].ip[n]-point[shape[h].p[0]].px)*ex[j] + (line[l].ip[n+1]-point[shape[h].p[0]].py)*ey[j] + (line[l].ip[n+2]-point[shape[h].p[0]].pz)*ez[j];
3391     sem_post(&sem_g);
3392               if(ptrans[j]>maxlhq[j]) maxlhq[j]=ptrans[j];
3393               if(ptrans[j]<minlhq[j]) minlhq[j]=ptrans[j];
3394   	    }
3395   	  }
3396         }
3397       }
3398     }
3399   }
3400   else
3401   {
3402     if(shape[h].type==2)
3403     {
3404       for(j=0; j<3; j++)
3405       {
3406       sem_wait(&sem_g);
3407         ptrans[j]=(point[shape[h].p[1]].px-point[shape[h].p[0]].px)*ex[j] + (point[shape[h].p[1]].py-point[shape[h].p[0]].py)*ey[j] + (point[shape[h].p[1]].pz-point[shape[h].p[0]].pz)*ez[j];
3408       sem_post(&sem_g);
3409         if(ptrans[j]>maxlhq[j]) maxlhq[j]=ptrans[j];
3410         if(ptrans[j]<minlhq[j]) minlhq[j]=ptrans[j];
3411       sem_wait(&sem_g);
3412         ptrans[j]=-2.;
3413       sem_post(&sem_g);
3414         if(ptrans[j]>maxlhq[j]) maxlhq[j]=ptrans[j];
3415         if(ptrans[j]<minlhq[j]) minlhq[j]=ptrans[j];
3416       }
3417     }
3418   }
3419   //for(j=0; j<3; j++) printf("maxlhq[%d]:%lf minlhq[%d]:%lf\n", j, maxlhq[j],j,minlhq[j]);
3420 
3421   /* generation of the nurbs */
3422   /* points */
3423   //for(i=0; i<8; i++) p_lhq[i][0]=(maxlhq[0]-minlhq[0])*-0.1;
3424   //for(i=8; i<16;i++) p_lhq[i][0]=(maxlhq[0]-minlhq[0])*1.1;
3425 
3426   /* The nurbs has to be adapted to the dimensions of the surf to make sure that a later trimming is possible */
3427   /* cone: scale also r1 and r2 accordingly */
3428   dl=v_betrag(p1);
3429   dr=rad2-rad1;
3430   qdrdl=dr/dl;
3431   dr2=maxlhq[0]*qdrdl;
3432   rad2=rad1+dr2;
3433   dr1=minlhq[0]*qdrdl;
3434   rad1+=dr1;
3435   //printf("r1,2 %f %f\n",rad1,rad2);
3436 
3437   for(i=0; i<8; i++) p_lhq[i][0]=minlhq[0];
3438   p_lhq[0][1]=-rad1;
3439   p_lhq[1][1]=-rad1;
3440   p_lhq[2][1]=0.     ;
3441   p_lhq[3][1]= rad1;
3442   p_lhq[4][1]= rad1;
3443   p_lhq[5][1]= rad1;
3444   p_lhq[6][1]=0.     ;
3445   p_lhq[7][1]=-rad1;
3446   p_lhq[0][2]=0.     ;
3447   p_lhq[1][2]=-rad1;
3448   p_lhq[2][2]=-rad1;
3449   p_lhq[3][2]=-rad1;
3450   p_lhq[4][2]=0.     ;
3451   p_lhq[5][2]= rad1;
3452   p_lhq[6][2]= rad1;
3453   p_lhq[7][2]= rad1;
3454   for(i=8; i<16;i++) p_lhq[i][0]=maxlhq[0];
3455   p_lhq[8][1]=-rad2;
3456   p_lhq[9][1]=-rad2;
3457   p_lhq[10][1]=0.     ;
3458   p_lhq[11][1]= rad2;
3459   p_lhq[12][1]= rad2;
3460   p_lhq[13][1]= rad2;
3461   p_lhq[14][1]=0.     ;
3462   p_lhq[15][1]=-rad2;
3463   p_lhq[8][2]=0.     ;
3464   p_lhq[9][2]=-rad2;
3465   p_lhq[10][2]=-rad2;
3466   p_lhq[11][2]=-rad2;
3467   p_lhq[12][2]=0.     ;
3468   p_lhq[13][2]= rad2;
3469   p_lhq[14][2]= rad2;
3470   p_lhq[15][2]= rad2;
3471 
3472   //for(i=0; i<16; i++) for(j=0; j<3; j++) printf("p_lhq[%d]:%lf\n", i, p_lhq[i][j] );
3473 
3474   /* transform in xyz */
3475   for(i=0; i<16; i++) for(j=0; j<3; j++) p_xyz[i][j]=p_lhq[i][0]*el[j]+p_lhq[i][1]*eh[j]+p_lhq[i][2]*eq[j];
3476   /* offset */
3477   sem_wait(&sem_g);
3478   for(i=0; i<16; i++) { p_xyz[i][0]+=point[shape[h].p[0]].px; p_xyz[i][1]+=point[shape[h].p[0]].py; p_xyz[i][2]+=point[shape[h].p[0]].pz; }
3479   sem_post(&sem_g);
3480 
3481   //for(i=0; i<16; i++) for(j=0; j<3; j++) printf("p_xyz[%d]:%lf\n", i, p_xyz[i][j] );
3482 
3483   k=0;
3484   for(j=0; j<2; j++) //v
3485   {
3486     for(i=0; i<8; i++)  //u
3487    {
3488     sprintf(buffer,"-pa%d", k+1);
3489     sem_wait(&sem_g);
3490     pbuf[i][j]=pnt( buffer , p_xyz[k][0], p_xyz[k][1], p_xyz[k][2], 0 );
3491     sem_post(&sem_g);
3492     k++;
3493    }
3494   }
3495   pbuf[8][0]=pbuf[0][0];
3496   pbuf[8][1]=pbuf[0][1];
3497 
3498   /* nurbs */
3499 
3500   sem_wait(&sem_g);
3501 
3502   // use the shape pool to avoid conflicts with the master shape name
3503   S= getNewName( buffer, "sh" );
3504   if ( S == -1 )
3505     { printf(" ERROR: could not create new nurs\n"); return(-1); }
3506   if ((nurbs = (Nurbs *)realloc( (Nurbs *)nurbs, (anzGeo->nurs+1)*sizeof(Nurbs)) ) == NULL )
3507   { printf("\n\nERROR: realloc failure in Nurs, nurbs:%s not installed\n\n", buffer); return(-1); }
3508 
3509   S=anzGeo->nurs;
3510   hashNurs( sumAsci, buffer, S );
3511   anzGeo->nurs++;
3512   if((nurbs[S].name= (char *)malloc((strlen(buffer)+1)*sizeof(char))) == NULL )
3513   { printf("ERROR: malloc failed\n\n" ); return(-1); }
3514   strcpy(nurbs[S].name, buffer);
3515 
3516   if(printFlag) printf("create NURBS:%s\n",nurbs[S].name);
3517 
3518   nurbs[S].u_exp = 2;
3519   nurbs[S].v_exp = 1;
3520   nurbs[S].u_npnt= 9;
3521   nurbs[S].v_npnt= 2;
3522   nurbs[S].u_nknt= 12;
3523   nurbs[S].v_nknt= 4;
3524   nurbs[S].u_stride= 4* nurbs[S].v_npnt;
3525   nurbs[S].v_stride= 4;
3526 
3527   if ( (nurbs[S].uknt = (GLfloat *)malloc( (nurbs[S].u_nknt+1) * sizeof(GLfloat))) == NULL )
3528     printf("\n\n ERROR: realloc failed uknt\n\n");
3529   if ( (nurbs[S].vknt = (GLfloat *)malloc( (nurbs[S].v_nknt+1) * sizeof(GLfloat))) == NULL )
3530     printf("\n\n ERROR: realloc failed vknt\n\n");
3531   j=0;
3532   for(i=0; i<3; i++) nurbs[S].uknt[j++]=0.;
3533   for(i=0; i<2; i++) nurbs[S].uknt[j++]=1.;
3534   for(i=0; i<2; i++) nurbs[S].uknt[j++]=2.;
3535   for(i=0; i<2; i++) nurbs[S].uknt[j++]=3.;
3536   for(i=0; i<3; i++) nurbs[S].uknt[j++]=4.;
3537   j=0;
3538   for(i=0; i<2; i++) nurbs[S].vknt[j++]=0.;
3539   for(i=0; i<2; i++) nurbs[S].vknt[j++]=1.;
3540 
3541   if ( (nurbs[S].ctlpnt =
3542     (int **)malloc(  (nurbs[S].u_npnt+1) * sizeof(int *))) == NULL )
3543     printf("\n\n ERROR: malloc failed ctlpnt\n\n");
3544   for (i=0; i<nurbs[S].u_npnt; i++)
3545   {
3546     if ( (nurbs[S].ctlpnt[i] =
3547       (int *)malloc(  (nurbs[S].v_npnt+1) * sizeof( int ))) == NULL )
3548       printf("\n\n ERROR: malloc failed ctlpnt[i]\n\n");
3549     for (j=0; j<nurbs[S].v_npnt; j++)
3550     {
3551       nurbs[S].ctlpnt[i][j] = pbuf[i][j];
3552     }
3553   }
3554 
3555   if ( (nurbs[S].weight = (float **)malloc(  (nurbs[S].u_npnt+1) * sizeof(float *))) == NULL )
3556     printf("\n\n ERROR: malloc failed weight\n\n");
3557   for (i=0; i<nurbs[S].u_npnt; i++)
3558   {
3559     if ( (nurbs[S].weight[i] = (float *)malloc(  (nurbs[S].v_npnt+1) * sizeof(float))) == NULL )
3560       printf("\n\n ERROR: malloc failed weight[i]\n\n");
3561   }
3562 
3563   k=0;
3564   for(j=0; j<2; j++) //v
3565   {
3566     for(i=0; i<8; i+=2) //u
3567     {
3568       nurbs[S].weight[i][j] = 1.;
3569       nurbs[S].weight[i+1][j] = .7071;
3570     }
3571   }
3572   nurbs[S].weight[8][0] = 1.;
3573   nurbs[S].weight[8][1] = 1.;
3574 
3575   /*
3576   for (i=0; i<nurbs[S].u_npnt; i++)
3577   {
3578     for (j=0; j<nurbs[S].v_npnt; j++) printf("%d %d %s %f\n", i,j, point[nurbs[S].ctlpnt[i][j]].name, nurbs[S].weight[i][j]);
3579   }
3580   for (i=0; i<nurbs[S].u_nknt; i++) printf("%d %f\n",i, nurbs[S].uknt[i]);
3581   for (i=0; i<nurbs[S].v_nknt; i++) printf("%d %f\n",i, nurbs[S].vknt[i]);
3582   */
3583 
3584   nurbs[S].ctlarray=(GLfloat *)NULL;
3585   nurbs[S].endFlag=1;
3586   nurbs[S].type=GL_MAP2_VERTEX_4;
3587 
3588   /* additional variables for the trimming */
3589   nurbs[S].trimFlag=0;
3590   nurbs[S].patches=0;
3591   nurbs[S].nc=NULL;
3592   nurbs[S].uv=NULL;
3593   nurbs[S].xyz=NULL;
3594   nurbs[S].np=NULL;
3595   nurbs[S].umax=NULL;
3596   nurbs[S].vmax=NULL;
3597   nurbs[S].ustep=NULL;
3598   nurbs[S].vstep=NULL;
3599   nurbs[S].Nurb = (GLUnurbsObj *)gluNewNurbsRenderer();
3600   nurbs[S].nurbsType=1;
3601   nurbs[S].uvflipped=NULL;
3602   nurbs[S].sum_ambiguousPnts=NULL;
3603   repNurs(S);
3604   sem_post(&sem_g);
3605 
3606   return(S);
3607 }
3608 
3609 
surfToNurs(int s)3610 int surfToNurs(int s)
3611 {
3612   int pbuf[4], lbuf[4], sbuf,Sbuf, i,j,n,l,c;
3613   double p1[3], p2[3], el[3], eh[3], eq[3], nr[3];
3614   double ptrans[3], dlhq;
3615   double maxlhq[3]={-MAX_FLOAT,-MAX_FLOAT,-MAX_FLOAT}, minlhq[3]={MAX_FLOAT,MAX_FLOAT,MAX_FLOAT};
3616   double ex[3], ey[3], ez[3], vx[3]={1.,0.,0.}, vy[3]={0.,1.,0.}, vz[3]={0.,0.,1.};
3617   double p_xyz[4][3], p_lhq[4][3];
3618   char buffer[MAX_LINE_LENGTH];
3619 
3620   extern sem_t   sem_stn;
3621 
3622   //printf("surf:%s\n", surf[s].name);
3623 
3624   if((shape[surf[s].sh].type==1)||(shape[surf[s].sh].type==2)) return(coneToNurs(s,1));
3625   else if(shape[surf[s].sh].type==3) return(sphToNurs(s,1));
3626   else if(shape[surf[s].sh].type==4) return(-1);
3627   else if(shape[surf[s].sh].type==5) return(torusToNurs(s,1));
3628 
3629   /* default is pln */
3630 
3631   /* generate 3 points on the shape which cover the drawing space */
3632   sem_wait(&sem_g);
3633   v_result( &point[shape[surf[s].sh].p[0]].px, &point[shape[surf[s].sh].p[1]].px, p1);
3634   v_result( &point[shape[surf[s].sh].p[0]].px, &point[shape[surf[s].sh].p[2]].px, p2);
3635   sem_post(&sem_g);
3636 
3637   /* generate 2 perpendicular vectors in this shape */
3638   v_prod( p1, p2, nr );
3639   v_prod( nr, p1, p2 );
3640 
3641   v_norm( p1, el );
3642   v_norm( p2, eh );
3643   v_norm( nr, eq );
3644 
3645   /* transform the xyz unit-vectors into the lhq system */
3646   ex[0]=v_sprod(vx,el);
3647   ex[1]=v_sprod(vx,eh);
3648   ex[2]=v_sprod(vx,eq);
3649   ey[0]=v_sprod(vy,el);
3650   ey[1]=v_sprod(vy,eh);
3651   ey[2]=v_sprod(vy,eq);
3652   ez[0]=v_sprod(vz,el);
3653   ez[1]=v_sprod(vz,eh);
3654   ez[2]=v_sprod(vz,eq);
3655 
3656   /* transform all points of the surface into lhq coordinates */
3657   for(i=0; i<surf[s].nl; i++)
3658   {
3659     if(surf[s].typ[i]=='l')
3660     {
3661       l=surf[s].l[i];
3662       for (n=0; n<line[l].nip; n+=3)
3663       {
3664         for(j=0; j<3; j++)
3665         {
3666           ptrans[j]=line[l].ip[n]*ex[j] + line[l].ip[n+1]*ey[j] + line[l].ip[n+2]*ez[j];
3667           if(ptrans[j]>maxlhq[j]) maxlhq[j]=ptrans[j];
3668           if(ptrans[j]<minlhq[j]) minlhq[j]=ptrans[j];
3669 	  }
3670 	}
3671     }
3672     else
3673     {
3674       for (c=0; c<lcmb[surf[s].l[i]].nl; c++)
3675 	{
3676         l=lcmb[surf[s].l[i]].l[c];
3677         for (n=0; n<line[l].nip; n+=3)
3678         {
3679           for(j=0; j<3; j++)
3680           {
3681             ptrans[j]=line[l].ip[n]*ex[j] + line[l].ip[n+1]*ey[j] + line[l].ip[n+2]*ez[j];
3682             if(ptrans[j]>maxlhq[j]) maxlhq[j]=ptrans[j];
3683             if(ptrans[j]<minlhq[j]) minlhq[j]=ptrans[j];
3684 	    }
3685 	  }
3686       }
3687     }
3688   }
3689   //for(j=0; j<3; j++) printf("maxlhq[%d]:%lf minlhq[%d]:%lf\n", j, maxlhq[j],j,minlhq[j]);
3690 
3691   /* The nurbs has to be extended a bit to make sure that a later trimming is possible */
3692   for(j=0; j<3; j++)
3693   {
3694     dlhq=(maxlhq[j]-minlhq[j])/2.*0.5;
3695     maxlhq[j]+=dlhq;
3696     minlhq[j]-=dlhq;
3697   }
3698   //for(j=0; j<3; j++) printf("maxlhq[%d]:%lf minlhq[%d]:%lf\n", j, maxlhq[j],j,minlhq[j]);
3699 
3700   /* create points from maxlhq and minlhq which enclose the surface (p1 1st quadrant, pn: math+) */
3701   p_lhq[0][0]=maxlhq[0];
3702   p_lhq[0][1]=maxlhq[1];
3703   p_lhq[0][2]=maxlhq[2];
3704   p_lhq[1][0]=minlhq[0];
3705   p_lhq[1][1]=maxlhq[1];
3706   p_lhq[1][2]=maxlhq[2];
3707   p_lhq[2][0]=minlhq[0];
3708   p_lhq[2][1]=minlhq[1];
3709   p_lhq[2][2]=maxlhq[2];
3710   p_lhq[3][0]=maxlhq[0];
3711   p_lhq[3][1]=minlhq[1];
3712   p_lhq[3][2]=maxlhq[2];
3713 
3714   /* transform in xyz */
3715   for(i=0; i<4; i++) for(j=0; j<3; j++) p_xyz[i][j]=p_lhq[i][0]*el[j]+p_lhq[i][1]*eh[j]+p_lhq[i][2]*eq[j];
3716   //for(i=0; i<4; i++) for(j=0; j<3; j++) printf("p_xyz[%d]:%lf\n", i, p_xyz[i][j] );
3717 
3718   // only one thread per time allowed here (common names):
3719   if(sem_wait(&sem_stn)) printf("Error in:sem_wait\n");
3720   for(i=0; i<4; i++)
3721   {
3722     sprintf(buffer,"-pa%d", i+1);
3723     sem_wait(&sem_g);
3724     pbuf[i]=pnt( buffer , p_xyz[i][0], p_xyz[i][1], p_xyz[i][2], 0 );
3725     sem_post(&sem_g);
3726   }
3727   for(i=0; i<3; i++)
3728   {
3729     sprintf(buffer,"-l%d", i+1);
3730     // no new allocated line is allowed (threading), make sure a deleted line is available
3731     lbuf[i]=line_i( buffer, pbuf[i], pbuf[i+1], 0, 1, 1, 0 );
3732   }
3733   sprintf(buffer,"-l%d", i+1);
3734   // no new allocated line is allowed (threading), make sure a deleted line is available
3735   lbuf[i]=line_i( buffer, pbuf[i], pbuf[0], 0, 1, 1, 0 );
3736 
3737   // no new allocated surf is allowed (threading), make sure a deleted surf is available
3738   //printf("hallo surf2Nurs surf:%s ptr:%x\n", surf[s].name, surf);
3739   sbuf=surface_i( "-1s", '+', -1, (int)4, "++++", lbuf, "llll");
3740   //printf("hallo surf2Nurs surf:%s ptr:%x\n", surf[s].name, surf);
3741 
3742   Sbuf=createBlendedNurbs(sbuf);
3743 
3744   // was deactivated??
3745   repNurs(Sbuf );
3746 
3747   /* delete the temp.surf,line */
3748   delPnt( 4, pbuf );
3749   delLine( 4, lbuf );
3750   delSurf( 1, &sbuf );
3751   if(sem_post(&sem_stn)) printf("Error in:sem_post\n");
3752 
3753   return(Sbuf);
3754 }
3755 
3756 
descalAll(void)3757 void descalAll(void)
3758 {
3759   descalNodes ( anz->n, node, scale );
3760   descalPoints( anzGeo->p, point, scale);
3761   descalSurfs( anzGeo->s, surf, scale);
3762   scale->x=0.;
3763   scale->y=0.;
3764   scale->z=0.;
3765   scale->w=1.;
3766 }
3767 
3768 
3769 /*  store the polygons for illuminated rendering */
3770 /* Warning: changes *scale */
repShape(int setNr)3771 int repShape(int setNr )
3772 {
3773   int n=0, nr,j;
3774   double pa1[3], pa2[3], ps1[3], ps2[3];
3775   double l_offs;
3776   double p1[3], p2[3], pb[3];
3777   double el[3], eh[3];
3778 
3779   if(set[setNr].anz_sh==0) return(0);
3780 
3781   /* the drawing space has to be scaled to 2*2 */
3782   //descalShapes( anzGeo->sh, shape, scale); TBD
3783   descalAll();
3784   getScaleValues( 0, set, point, node, scale);
3785   scalNodes ( anz->n, node, scale );
3786   scalPoints( anzGeo->p, point, scale);
3787   scalSurfs( anzGeo->s, surf, scale);
3788 
3789 
3790   for (j=0; j<set[setNr].anz_sh; j++)
3791   {
3792     nr=set[setNr].shp[j];
3793 
3794     if(printFlag) printf("shape:%s type:%d\n", shape[nr].name, shape[nr].type);
3795 
3796     if( shape[nr].type == 0)
3797     {
3798       /* generate 3 points on the shape which cover the drawing space */
3799       v_result( &point[shape[nr].p[0]].px, &point[shape[nr].p[1]].px, p1);
3800       v_result( &point[shape[nr].p[0]].px, &point[shape[nr].p[2]].px, p2);
3801 
3802       /* generate 2 perpendicular vectors in this shape */
3803       v_prod( p1, p2, pb );
3804       v_prod( pb, p1, p2 );
3805 
3806       v_norm( p1, el );
3807       v_norm( p2, eh );
3808       l_offs=2.;
3809       v_scal(&l_offs, el, p1);
3810       v_add(&point[shape[nr].p[0]].px, p1, pa1);
3811       v_scal(&l_offs, eh, p1);
3812       v_add(&point[shape[nr].p[0]].px, p1, ps1);
3813       l_offs=-2.;
3814       v_scal(&l_offs, el, p1);
3815       v_add(&point[shape[nr].p[0]].px, p1, pa2);
3816       v_scal(&l_offs, eh, p1);
3817       v_add(&point[shape[nr].p[0]].px, p1, ps2);
3818 
3819       //printf("ps2: %f %f %f\n", ps2[0], ps2[1], ps2[2]);
3820 
3821       /* alloc a new tri */
3822       shape[nr].npgn=36;
3823       if((shape[nr].pgn=(GLdouble *)realloc((GLdouble *)shape[nr].pgn, shape[nr].npgn*sizeof(GLdouble)) )==NULL)
3824       {
3825         errMsg("\nERROR: realloc failed in repShape() \n\n");
3826       }
3827       n=0;
3828       shape[nr].pgn[n++]=ps2[0];
3829       shape[nr].pgn[n++]=ps2[1];
3830       shape[nr].pgn[n++]=ps2[2];
3831       shape[nr].pgn[n++]=pa1[0];
3832       shape[nr].pgn[n++]=pa1[1];
3833       shape[nr].pgn[n++]=pa1[2];
3834       shape[nr].pgn[n++]=point[shape[nr].p[0]].px;
3835       shape[nr].pgn[n++]=point[shape[nr].p[0]].py;
3836       shape[nr].pgn[n++]=point[shape[nr].p[0]].pz;
3837       shape[nr].pgn[n++]=pa1[0];
3838       shape[nr].pgn[n++]=pa1[1];
3839       shape[nr].pgn[n++]=pa1[2];
3840       shape[nr].pgn[n++]=ps1[0];
3841       shape[nr].pgn[n++]=ps1[1];
3842       shape[nr].pgn[n++]=ps1[2];
3843       shape[nr].pgn[n++]=point[shape[nr].p[0]].px;
3844       shape[nr].pgn[n++]=point[shape[nr].p[0]].py;
3845       shape[nr].pgn[n++]=point[shape[nr].p[0]].pz;
3846       shape[nr].pgn[n++]=ps1[0];
3847       shape[nr].pgn[n++]=ps1[1];
3848       shape[nr].pgn[n++]=ps1[2];
3849       shape[nr].pgn[n++]=pa2[0];
3850       shape[nr].pgn[n++]=pa2[1];
3851       shape[nr].pgn[n++]=pa2[2];
3852       shape[nr].pgn[n++]=point[shape[nr].p[0]].px;
3853       shape[nr].pgn[n++]=point[shape[nr].p[0]].py;
3854       shape[nr].pgn[n++]=point[shape[nr].p[0]].pz;
3855       shape[nr].pgn[n++]=pa2[0];
3856       shape[nr].pgn[n++]=pa2[1];
3857       shape[nr].pgn[n++]=pa2[2];
3858       shape[nr].pgn[n++]=ps2[0];
3859       shape[nr].pgn[n++]=ps2[1];
3860       shape[nr].pgn[n++]=ps2[2];
3861       shape[nr].pgn[n++]=point[shape[nr].p[0]].px;
3862       shape[nr].pgn[n++]=point[shape[nr].p[0]].py;
3863       shape[nr].pgn[n++]=point[shape[nr].p[0]].pz;
3864     }
3865     else
3866     {
3867       if(printFlag) printf(" WARNING: shape:%s type:%d, surface rendering for this type is not implemented.\n", shape[nr].name, shape[nr].type );
3868       return(0);
3869     }
3870   }
3871   return(1);
3872 }
3873 
3874 
3875 
calcTrimLoops(int nurbsnr,int nr)3876 int calcTrimLoops(int nurbsnr, int nr)
3877 {
3878   int i,j,l,cl,nip,flag;
3879   int n;    /* running number through all inner-points ip in the line-def */
3880   int k=0;  /* running number through all corners in the surf-def */
3881   int p;    /* running number through all points of each closed curve (trimming-loops) of the surf */
3882   int cp;    /* running number through all points of each closed curve (trimming-loops) of the surf */
3883   double *lcurve=NULL; /* tracks the length of all closed curves, the biggest is the outer loop. */
3884   double *clmax=NULL;  /* tracks the loop-direction (ccw or cw */
3885   double *nu=NULL, *nv=NULL;  /* sum of the u and v coordinates for all curves, averaged */
3886   GLdouble *knt=NULL;           /* knot-buffer to invert nurbs-curves if necessary */
3887   double p0[3], p0p1[3], p0p2[3], p1[3], p2[3], p1p2[3], lp1p2, lmax=0.;
3888   double vn[3];
3889   int c_outer=0, patch;
3890   int nclp;
3891   double *xyz=NULL;
3892 
3893   int firstl, lastl;
3894   int *linbuf=NULL;
3895   char *oribuf=NULL;
3896 
3897   double tol_ambig;
3898   int nurbsbuf[2];
3899 
3900 
3901   /* redefine the nurbs (deactivated: ->ERROR: forbidden if a degree reduction was performed, the control-points were not updated!)*/
3902   //repNurs(nurbsnr);
3903   sem_wait(&sem_g);
3904 
3905   patch=surf[nr].patch=nurbs[nurbsnr].patches;
3906   nurbs[nurbsnr].patches++;
3907   printf("surf:%s NURBS:%s patch%d\n",surf[nr].name, nurbs[nurbsnr].name, patch);
3908 
3909   if( (nurbs[nurbsnr].umax= (double *)realloc( (double *)nurbs[nurbsnr].umax, (nurbs[nurbsnr].patches)*sizeof(double) )) == NULL )
3910   { printf(" ERROR: realloc failure0, nurbs:%s can not be shaped\n\n", nurbs[nurbsnr].name);
3911     return(-1); }
3912   if( (nurbs[nurbsnr].vmax= (double *)realloc( (double *)nurbs[nurbsnr].vmax, (nurbs[nurbsnr].patches)*sizeof(double) )) == NULL )
3913   { printf(" ERROR: realloc failure1, nurbs:%s can not be shaped\n\n", nurbs[nurbsnr].name);
3914     return(-1); }
3915   if( (nurbs[nurbsnr].ustep= (GLfloat *)realloc( (GLfloat *)nurbs[nurbsnr].ustep, (nurbs[nurbsnr].patches)*sizeof(GLfloat) )) == NULL )
3916   { printf(" ERROR: realloc failure, nurbs:%s can not be shaped\n\n", nurbs[nurbsnr].name);
3917     return(-1); }
3918   if( (nurbs[nurbsnr].vstep= (GLfloat *)realloc( (GLfloat *)nurbs[nurbsnr].vstep, (nurbs[nurbsnr].patches)*sizeof(GLfloat) )) == NULL )
3919   { printf(" ERROR: realloc failure, nurbs:%s can not be shaped\n\n", nurbs[nurbsnr].name);
3920     return(-1); }
3921   if( (nurbs[nurbsnr].nc= (int *)realloc( (int *)nurbs[nurbsnr].nc, (nurbs[nurbsnr].patches)*sizeof(int) )) == NULL )
3922   { printf(" ERROR: realloc failure2, nurbs:%s can not be shaped\n\n", nurbs[nurbsnr].name);
3923     return(-1); }
3924   if( (nurbs[nurbsnr].np= (int **)realloc( (int **)nurbs[nurbsnr].np, (nurbs[nurbsnr].patches)*sizeof(int *) )) == NULL )
3925   { printf(" ERROR: realloc failure3, nurbs:%s can not be shaped\n\n", nurbs[nurbsnr].name);
3926     return(-1); }
3927   if( (nurbs[nurbsnr].np[patch]= (int *)malloc( (surf[nr].nc)*sizeof(int) )) == NULL )
3928   { printf(" ERROR: realloc failure4, nurbs:%s can not be shaped\n\n", nurbs[nurbsnr].name);
3929     return(-1); }
3930 
3931   if( (nurbs[nurbsnr].uv = (GLfloat ***)realloc( (GLfloat ***)nurbs[nurbsnr].uv, (nurbs[nurbsnr].patches)*sizeof(GLfloat **) )) == NULL )
3932   { printf(" ERROR: realloc failure8, nurbs:%s can not be shaped\n\n", nurbs[nurbsnr].name);
3933     return(-1); }
3934   if( (nurbs[nurbsnr].uv[patch] = (GLfloat **)malloc( (surf[nr].nc+1)*sizeof(GLfloat *) )) == NULL )
3935   { printf(" ERROR: realloc failure9, nurbs:%s can not be shaped\n\n", nurbs[nurbsnr].name);
3936     return(-1); }
3937   if( (nurbs[nurbsnr].xyz = (double ***)realloc( (double ***)nurbs[nurbsnr].xyz, (nurbs[nurbsnr].patches)*sizeof(double **) )) == NULL )
3938   { printf(" ERROR: realloc failure10, nurbs:%s can not be shaped\n\n", nurbs[nurbsnr].name);
3939     return(-1); }
3940   if( (nurbs[nurbsnr].xyz[patch] = (double **)malloc( (surf[nr].nc+1)*sizeof(double *) )) == NULL )
3941   { printf(" ERROR: realloc failure11, nurbs:%s can not be shaped\n\n", nurbs[nurbsnr].name);
3942     return(-1); }
3943 
3944   if( (nurbs[nurbsnr].uvflipped = (int **)realloc( (int **)nurbs[nurbsnr].uvflipped, (nurbs[nurbsnr].patches)*sizeof(int *) )) == NULL )
3945   { printf(" ERROR: realloc failure10, nurbs:%s can not be shaped\n\n", nurbs[nurbsnr].name);
3946     return(-1); }
3947   if( (nurbs[nurbsnr].uvflipped[patch] = (int *)calloc( (surf[nr].nc+1),sizeof(int) )) == NULL )
3948   { printf(" ERROR: realloc failure11, nurbs:%s can not be shaped\n\n", nurbs[nurbsnr].name);
3949     return(-1); }
3950   if( (nurbs[nurbsnr].sum_ambiguousPnts = (int **)realloc( (int **)nurbs[nurbsnr].sum_ambiguousPnts, (nurbs[nurbsnr].patches)*sizeof(int *) )) == NULL )
3951   { printf(" ERROR: realloc failure10, nurbs:%s can not be shaped\n\n", nurbs[nurbsnr].name);
3952     return(-1); }
3953   if( (nurbs[nurbsnr].sum_ambiguousPnts[patch] = (int *)calloc( (surf[nr].nc+1),sizeof(int) )) == NULL )
3954   { printf(" ERROR: realloc failure11, nurbs:%s can not be shaped\n\n", nurbs[nurbsnr].name);
3955     return(-1); }
3956 
3957   if( (lcurve= (double *)realloc( (double *)lcurve, (surf[nr].nc+1)*sizeof(double) )) == NULL )
3958   { printf(" ERROR: realloc failure5, nurbs:%s can not be shaped\n\n", nurbs[nurbsnr].name);
3959     return(-1); }
3960   if( (clmax= (double *)realloc( (double *)clmax, (surf[nr].nc+1)*sizeof(double) )) == NULL )
3961   { printf(" ERROR: realloc failure5, nurbs:%s can not be shaped\n\n", nurbs[nurbsnr].name);
3962     return(-1); }
3963   if( (nu= (double *)realloc( (double *)nu, (surf[nr].nc+1)*sizeof(double) )) == NULL )
3964   { printf(" ERROR: realloc failure6, nurbs:%s can not be shaped\n\n", nurbs[nurbsnr].name);
3965     return(-1); }
3966   if( (nv= (double *)realloc( (double *)nv, (surf[nr].nc+1)*sizeof(double) )) == NULL )
3967   { printf(" ERROR: realloc failure7, nurbs:%s can not be shaped\n\n", nurbs[nurbsnr].name);
3968     return(-1); }
3969   for (i=0; i<surf[nr].nc; i++) lcurve[i]=nu[i]=nv[i]=0.;
3970 
3971  newTrimming:;
3972 
3973   k=0;
3974   for (i=0; i<surf[nr].nc; i++)
3975   {
3976     nip=0;
3977     for(j=0; j<surf[nr].c[i]; j++)
3978     {
3979       if(surf[nr].typ[k]=='l')
3980       {
3981         l=surf[nr].l[k];
3982         nip+=line[l].nip;
3983       }
3984       else
3985       {
3986         nclp=0;
3987         for(cl=0; cl<lcmb[surf[nr].l[k]].nl; cl++)
3988 	{
3989           l=lcmb[surf[nr].l[k]].l[cl];
3990           if(lcmb[surf[nr].l[k]].o[cl]=='-') flag=-1;
3991           else flag=1;
3992 
3993           if(flag==1)
3994           {
3995             if(cl==0)                     { n=0; flag=line[l].nip-3; }
3996             else if(cl==lcmb[surf[nr].l[k]].nl-1)    { n=0; flag=line[l].nip; }
3997             else                         { n=0; flag=line[l].nip-3; }
3998             do
3999             {
4000                n++; nclp++;
4001             }while(n<flag);
4002           }
4003           else
4004           {
4005             if(cl==0)                     { n=line[l].nip; flag=3; }
4006             else if(cl==lcmb[surf[nr].l[k]].nl-1)    { n=line[l].nip; flag=0; }
4007             else                         { n=line[l].nip; flag=3; }
4008             while(n>flag)
4009             {
4010               n-=3;
4011               nclp+=3;
4012             }
4013           }
4014 	}
4015         nip+=nclp;
4016       }
4017       k++;
4018     }
4019     // calloc((nip+xx) NURS_ADD_AMBIG_PNTS points more to cover ambiguous points (should be sufficient)
4020     //printf("nip:%d\n",nip);
4021     if( (nurbs[nurbsnr].uv[patch][i] = (GLfloat *)calloc((nip+NURS_ADD_AMBIG_PNTS*2),sizeof(GLfloat) ))==NULL )
4022     { printf(" ERROR: realloc failure12, nurbs:%s can not be shaped\n\n", nurbs[nurbsnr].name); return(-1); }
4023     if( (nurbs[nurbsnr].xyz[patch][i] = (double *)calloc((nip+NURS_ADD_AMBIG_PNTS*3),sizeof(double ) ))==NULL )
4024     { printf(" ERROR: realloc failure13, nurbs:%s can not be shaped\n\n", nurbs[nurbsnr].name); return(-1); }
4025   }
4026   sem_post(&sem_g);
4027 
4028   /* from here on a trimming attempt is made if necessary and repeated if failed */
4029   tol_ambig=TOL_AMBIG;
4030   k=0;
4031   for (i=0; i<surf[nr].nc; i++)
4032   {
4033     p=0;
4034     cp=0;
4035     nip=0;
4036     for(j=0; j<surf[nr].c[i]; j++)
4037     {
4038       /* store the locations of all line-points, they will be projected to the NURBS */
4039       if(surf[nr].typ[k]=='l')
4040       {
4041         l=surf[nr].l[k];
4042         nip+=line[l].nip;
4043 
4044         if(surf[nr].o[k]=='+')
4045         {
4046           if(j==0)                     { n=0; flag=line[l].nip-3; }
4047           else if(j==surf[nr].c[i]-1)    { n=0; flag=line[l].nip; }
4048           else                         { n=0; flag=line[l].nip-3; }
4049           do
4050           {
4051   sem_wait(&sem_g);
4052             nurbs[nurbsnr].xyz[patch][i][p]=line[l].ip[n++];
4053   sem_post(&sem_g);
4054             p++;
4055 #if TEST
4056 	    if(p%3==0)
4057 	      {
4058         printf("patch%d i%d p%d\n", patch,i,p);
4059 	printf("pnt ! %f %f %f\n", nurbs[nurbsnr].xyz[patch][i][p-3]*scale->w+scale->x,nurbs[nurbsnr].xyz[patch][i][p-2]*scale->w+scale->y,nurbs[nurbsnr].xyz[patch][i][p-1]*scale->w+scale->z);
4060 	      }
4061 #endif
4062           }while(n<flag);
4063         }
4064         else
4065         {
4066           if(j==0)                     { n=line[l].nip; flag=3; }
4067           else if(j==surf[nr].c[i]-1)    { n=line[l].nip; flag=0; }
4068           else                         { n=line[l].nip; flag=3; }
4069           while(n>flag)
4070           {
4071   sem_wait(&sem_g);
4072             nurbs[nurbsnr].xyz[patch][i][p+2]=line[l].ip[--n];
4073             nurbs[nurbsnr].xyz[patch][i][p+1]=line[l].ip[--n];
4074             nurbs[nurbsnr].xyz[patch][i][p]=line[l].ip[--n];
4075   sem_post(&sem_g);
4076             p+=3;
4077 #if TEST
4078 	    if(p%3==0)
4079 	      {
4080         printf("patch%d i%d p%d\n", patch,i,p);
4081 	printf("pnt ! %f %f %f\n", nurbs[nurbsnr].xyz[patch][i][p-3]*scale->w+scale->x,nurbs[nurbsnr].xyz[patch][i][p-2]*scale->w+scale->y,nurbs[nurbsnr].xyz[patch][i][p-1]*scale->w+scale->z);
4082 	      }
4083 #endif
4084           }
4085         }
4086       }
4087       else
4088       {
4089         /* first generate an array of internal points of all lines */
4090         nclp=0;
4091         for(cl=0; cl<lcmb[surf[nr].l[k]].nl; cl++)
4092 	{
4093           l=lcmb[surf[nr].l[k]].l[cl];
4094 
4095           if( (xyz = (double *)realloc( (double *)xyz, (nclp+line[l].nip)*sizeof(double ) ))==NULL )
4096           { printf(" ERROR: realloc failure15, nurbs:%s can not be shaped\n\n", nurbs[nurbsnr].name);
4097             return(-1); }
4098 
4099           if(lcmb[surf[nr].l[k]].o[cl]=='-') flag=-1;
4100           else flag=1;
4101 
4102           if(flag==1)
4103           {
4104             if(cl==0)                     { n=0; flag=line[l].nip-3; }
4105             else if(cl==lcmb[surf[nr].l[k]].nl-1)    { n=0; flag=line[l].nip; }
4106             else                         { n=0; flag=line[l].nip-3; }
4107             do
4108             {
4109               xyz[nclp]=line[l].ip[n++];
4110               nclp++;
4111             }while(n<flag);
4112           }
4113           else
4114           {
4115             if(cl==0)                     { n=line[l].nip; flag=3; }
4116             else if(cl==lcmb[surf[nr].l[k]].nl-1)    { n=line[l].nip; flag=0; }
4117             else                         { n=line[l].nip; flag=3; }
4118             while(n>flag)
4119             {
4120               xyz[nclp+2]=line[l].ip[--n];
4121               xyz[nclp+1]=line[l].ip[--n];
4122               xyz[nclp]=line[l].ip[--n];
4123               nclp+=3;
4124             }
4125           }
4126 	}
4127 
4128         /* then add this points to the line-loop */
4129         nip+=nclp;
4130 
4131         if(surf[nr].o[k]=='+')
4132         {
4133           if(j==0)                     { n=0; flag=nclp-3; }
4134           else if(j==surf[nr].c[i]-1)    { n=0; flag=nclp; }
4135           else                         { n=0; flag=nclp-3; }
4136           do
4137           {
4138   sem_wait(&sem_g);
4139             nurbs[nurbsnr].xyz[patch][i][p]=xyz[n++];
4140   sem_post(&sem_g);
4141             p++;
4142 #if TEST
4143 	    if(p%3==0)
4144 	      {
4145         printf("#+lcmb patch%d i%d p%d\n", patch,i,p);
4146 	printf("pnt ! %f %f %f\n", nurbs[nurbsnr].xyz[patch][i][p-3]*scale->w+scale->x,nurbs[nurbsnr].xyz[patch][i][p-2]*scale->w+scale->y,nurbs[nurbsnr].xyz[patch][i][p-1]*scale->w+scale->z);
4147 	      }
4148 #endif
4149           }while(n<flag);
4150         }
4151         else
4152         {
4153           if(j==0)                     { n=nclp; flag=3; }
4154           else if(j==surf[nr].c[i]-1)    { n=nclp; flag=0; }
4155           else                         { n=nclp; flag=3; }
4156           while(n>flag)
4157           {
4158   sem_wait(&sem_g);
4159             nurbs[nurbsnr].xyz[patch][i][p+2]=xyz[--n];
4160             nurbs[nurbsnr].xyz[patch][i][p+1]=xyz[--n];
4161             nurbs[nurbsnr].xyz[patch][i][p]=xyz[--n];
4162   sem_post(&sem_g);
4163             p+=3;
4164 #if TEST
4165         printf("#-lcmb patch%d i%d p%d\n",k, patch,i,p);
4166 	printf("pnt ! %f %f %f\n", nurbs[nurbsnr].xyz[patch][i][p-3]*scale->w+scale->x,nurbs[nurbsnr].xyz[patch][i][p-2]*scale->w+scale->y,nurbs[nurbsnr].xyz[patch][i][p-1]*scale->w+scale->z);
4167 #endif
4168           }
4169         }
4170       }
4171 
4172       k++;
4173     }
4174   sem_wait(&sem_g);
4175     nurbs[nurbsnr].np[patch][i]=p/3;
4176   sem_post(&sem_g);
4177   }
4178   sem_wait(&sem_g);
4179   nurbs[nurbsnr].nc[patch]=surf[nr].nc;
4180   sem_post(&sem_g);
4181 
4182   /* calc u,v coordinates for all trimming-points */
4183   calcNurbsResolution(nurbsnr);
4184   i=trimNurbs( nurbsnr, patch, tol_ambig);
4185   if(i!=0) return(i);
4186 
4187   for (i=0; i<surf[nr].nc; i++)
4188   {
4189     lcurve[i]=0;
4190     /* determine the average point, needed to define the orientation of the curve */
4191   sem_wait(&sem_g);
4192     p1[0]=nurbs[nurbsnr].uv[patch][i][0];
4193     p1[1]=nurbs[nurbsnr].uv[patch][i][1];
4194     nurbsbuf[0]=nurbs[nurbsnr].np[patch][i]*2;
4195   sem_post(&sem_g);
4196    nu[i]+=p1[0];
4197     nv[i]+=p1[1];
4198     p1[2]=p2[2]=0.;
4199     cp=2; do
4200     {
4201   sem_wait(&sem_g);
4202       p2[0]=nurbs[nurbsnr].uv[patch][i][cp++];
4203       p2[1]=nurbs[nurbsnr].uv[patch][i][cp++];
4204   sem_post(&sem_g);
4205       nu[i]+=p2[0];
4206       nv[i]+=p2[1];
4207       v_result( p1, p2, p1p2 );
4208       p1[0]=p2[0];
4209       p1[1]=p2[1];
4210       lp1p2=v_betrag( p1p2 );
4211       lcurve[i]+=lp1p2;
4212     }while(cp<nurbsbuf[0]);
4213     nu[i]/=cp/2;
4214     nv[i]/=cp/2;
4215 
4216     if(lcurve[i]>lmax) { lmax=lcurve[i]; c_outer=i; }
4217   }
4218   if(printFlag) printf("outer curve:%d\n",c_outer);
4219 
4220 
4221   /* if the outer loop c_outer is not the first one (0) then re-arrange the loops */
4222   if(c_outer!=0)
4223   {
4224 
4225     /* which lines are affected? */
4226     firstl=0; for(n=0; n<c_outer; n++) firstl+=surf[nr].c[n]; lastl=firstl+surf[nr].c[n];
4227 
4228     printf("l:%d 1st:%d last:%d\n",surf[nr].nl,firstl,lastl);
4229     for(n=0; n<surf[nr].nl-1; n++)
4230     {
4231       printf("%d ", surf[nr].l[n]);
4232     }
4233     printf("\n");
4234 
4235     if( (linbuf= (int *)malloc( (lastl-firstl+1)*sizeof(int) )) == NULL )
4236     { printf(" ERROR: realloc failure\n\n"); return(-1); }
4237     if( (oribuf= (char *)malloc( (lastl-firstl+1)*sizeof(char) )) == NULL )
4238     { printf(" ERROR: realloc failure\n\n"); return(-1); }
4239 
4240     j=0; for(l=n=surf[nr].nl-1; n>=0; n--)
4241     {
4242       if((n<lastl)&&(n>=firstl))
4243       {
4244         linbuf[j]=surf[nr].l[n];
4245         oribuf[j++]=surf[nr].o[n];
4246       }
4247       else
4248       {
4249         surf[nr].l[l]=surf[nr].l[n];
4250         surf[nr].o[l--]=surf[nr].o[n];
4251       }
4252     }
4253     j=0; for(n=l; n>=0; n--)
4254     {
4255       surf[nr].l[n]=linbuf[j];
4256       surf[nr].o[n]=oribuf[j++];
4257     }
4258     printf("\n");
4259     j=0; for(n=0; n<surf[nr].nl-1; n++)
4260     {
4261       printf("%d ", surf[nr].l[n]);
4262     }
4263     printf("\n");
4264     free(linbuf);
4265     free(oribuf);
4266     c_outer=0;
4267     orientSurf(nr);
4268     for (i=0; i<surf[nr].nc; i++)
4269     {
4270       free(nurbs[nurbsnr].uv[patch][i]);
4271       free(nurbs[nurbsnr].xyz[patch][i]);
4272     }
4273     goto newTrimming;
4274   }
4275 
4276   /* determine umax, vmax. Used in drawNurs_plot() */
4277   sem_wait(&sem_g);
4278   nurbs[nurbsnr].umax[patch]=nurbs[nurbsnr].vmax[patch]=-MAX_INTEGER;
4279   n=0; while(n<(nurbs[nurbsnr].np[patch][c_outer]*2))
4280   {
4281     if(nurbs[nurbsnr].umax[patch]<nurbs[nurbsnr].uv[patch][c_outer][n])
4282       nurbs[nurbsnr].umax[patch]=nurbs[nurbsnr].uv[patch][c_outer][n];
4283     n++;
4284     if(nurbs[nurbsnr].vmax[patch]<nurbs[nurbsnr].uv[patch][c_outer][n])
4285       nurbs[nurbsnr].vmax[patch]=nurbs[nurbsnr].uv[patch][c_outer][n];
4286     n++;
4287   }
4288   if(printFlag) printf("patch:%d uvw_cp outer curve:%lf %lf %lf maxu:%lf maxv:%lf\n", patch, nu[c_outer], nv[c_outer], lmax, nurbs[nurbsnr].umax[patch], nurbs[nurbsnr].vmax[patch]);
4289   sem_post(&sem_g);
4290 
4291   /* ORIENT-CHECK */
4292   /* check if the outer curve is correct oriented for trimming */
4293   /* the 1st has to be counter-clockwhise and the others clockwhise */
4294   /* calculate the normal-vector of the outer-curve in the uv space of the nurbs */
4295   /* if it points in -w direction (lmax<0) then uv of the Nurbs must be changed */
4296   sem_wait(&sem_g);
4297   nurbsbuf[0]=nurbs[nurbsnr].nc[patch];
4298   sem_post(&sem_g);
4299   for(i=0; i<nurbsbuf[0]; i++)
4300   {
4301     p0[0]=nu[i];
4302     p0[1]=nv[i];
4303     p0[2]=0.;
4304     p1[2]=0.;
4305     p2[2]=0.;
4306 	  //printf("seto curve%d\n pnt ! %f %f\n",i, p0[0],p0[1]);
4307     n=0; lmax=0.;
4308   sem_wait(&sem_g);
4309     nurbsbuf[1]=(nurbs[nurbsnr].np[patch][i]*2)-4;
4310   sem_post(&sem_g);
4311     do
4312     {
4313       /* add all normals based on uv[n] x uv[n+1] */
4314   sem_wait(&sem_g);
4315       p1[0]=nurbs[nurbsnr].uv[patch][i][n++];
4316       p1[1]=nurbs[nurbsnr].uv[patch][i][n++];
4317       p2[0]=nurbs[nurbsnr].uv[patch][i][n++];
4318       p2[1]=nurbs[nurbsnr].uv[patch][i][n++];
4319   sem_post(&sem_g);
4320       v_result(p0, p1, p0p1);
4321       v_result(p0, p2, p0p2);
4322       v_prod(p0p1,p0p2,vn);
4323       lmax+=vn[2];
4324       //printf("curve:%d p1:%lf %lf p2:%lf %lf vn2:%lf lmax:%lf\n",i, p1[0],p1[1], p2[0],p2[1],vn[2], lmax);
4325       //printf(" pnt ! %f %f\n", p1[0],p1[1]);
4326     }while(n<nurbsbuf[1]);
4327     //printf("setc\n# lmax:%lf\n",lmax);
4328 
4329     /* invert lmax for the inner curves for detection purposes*/
4330     if(i==c_outer)
4331     {
4332       /* detect the orientation of the nurbs relative to the surf with the outer loop */
4333       if(lmax<0) surf[nr].sho='-'; else surf[nr].sho='+';
4334     }
4335     else lmax*=-1.;
4336 
4337     if(lmax<0.)
4338     {
4339   sem_wait(&sem_g);
4340       nurbs[nurbsnr].uvflipped[patch][i]=1;
4341       /* orientation of the curve must be changed (change u,v) */
4342       if( (knt= (GLdouble *)realloc( (GLdouble *)knt, ((nurbs[nurbsnr].np[patch][i]*2)+1)*sizeof(GLdouble) )) == NULL )
4343       { printf(" ERROR: realloc failure16, nurbs:%s can not be shaped\n\n", nurbs[nurbsnr].name);
4344       return(-1); }
4345       for (j=0; j<nurbs[nurbsnr].np[patch][i]*2; j++) knt[j]=nurbs[nurbsnr].uv[patch][i][j];
4346       n=(nurbs[nurbsnr].np[patch][i]*2)-1; j=0;
4347       do
4348       {
4349         nurbs[nurbsnr].uv[patch][i][j++]=knt[n-1];
4350         nurbs[nurbsnr].uv[patch][i][j++]=knt[n];
4351         n-=2;
4352       }while(n>0);
4353   sem_post(&sem_g);
4354     }
4355     clmax[i]=lmax;
4356   }
4357 
4358   sem_wait(&sem_g);
4359   nurbsbuf[0]=nurbs[nurbsnr].nc[patch];
4360   sem_post(&sem_g);
4361   for(i=0; i<nurbsbuf[0]; i++)
4362   {
4363     lmax=clmax[i];
4364     /* change the orientation also in the surf-definition */
4365     if( (surf[nr].sho!='-')&&(surf[nr].sho!='+')) { printf("error sho :%c %d uninitialized in surf:%s. Talk to the programmer!\n", surf[nr].sho,surf[nr].sho,surf[nr].name); exit(0); }
4366     if(((lmax>0.)&&(surf[nr].sho=='-'))||((lmax<0.)&&(surf[nr].sho=='+')))
4367     {
4368       /* which lines are affected? */
4369       firstl=0; for(n=0; n<i; n++) firstl+=surf[nr].c[n]; lastl=firstl+surf[nr].c[n];
4370 
4371       if( (linbuf= (int *)malloc( (lastl-firstl+1)*sizeof(int) )) == NULL )
4372       { printf(" ERROR: realloc failure\n\n"); return(-1); }
4373       if( (oribuf= (char *)malloc( (lastl-firstl+1)*sizeof(char) )) == NULL )
4374       { printf(" ERROR: realloc failure\n\n"); return(-1); }
4375 
4376       j=0; for(n=firstl; n<lastl; n++)
4377       {
4378         linbuf[j]=surf[nr].l[n];
4379         if(surf[nr].o[n]=='+') oribuf[j++]='-'; else oribuf[j++]='+';
4380       }
4381       for(n=firstl; n<lastl; n++)
4382       {
4383         j--;
4384         surf[nr].l[n]=linbuf[j];
4385         surf[nr].o[n]=oribuf[j];
4386       }
4387       free(linbuf);
4388       free(oribuf);
4389     }
4390   }
4391   free(lcurve);
4392   free(clmax);
4393   free(nu);
4394   free(nv);
4395   free(knt);
4396   return(0);
4397 }
4398 
4399 
4400 
fillNurbsSurf(int nurbsnr,int nr)4401 int fillNurbsSurf(int nurbsnr, int nr)
4402 {
4403   int i,j,n,k;
4404   int nurbsbuf=0, oprod;
4405   GLint ipuf[2];
4406   double *fbuf=NULL;
4407   Nurbs nurb;
4408   int    patch;
4409 
4410   /* nurbs evaluation into the back-buffer */
4411   GLfloat *feedbackbuffer=NULL;
4412   GLint   size_fbb;
4413 
4414   /* the triangulation using GLu func works only with active graphics */
4415   if(nurbs[nurbsnr].endFlag==0)
4416   {
4417     printf(" WARNING: nurbs %s not valid\n",nurbs[nurbsnr].name);
4418     surf[nr].npgn=0;
4419     return(-1);
4420   }
4421   if(!inpformat) { surf[nr].npgn=0; return(-1); }
4422 
4423   /* check if the nurbs can be handled by the libGLU. */
4424   /* if not create an temporary approximation. The trias will be later corrected by the original nurbs */
4425   if(((nurbs[nurbsnr].u_exp>=gl_max_eval_order)||(nurbs[nurbsnr].v_exp>=gl_max_eval_order)))
4426   {
4427     if(printFlag) printf("WARNING: Nurbs:%s of order:%d %d will be redefined. Only %d is supported by the gl-lib.\n", nurbs[nurbsnr].name, nurbs[nurbsnr].u_exp+1, nurbs[nurbsnr].v_exp+1, gl_max_eval_order);
4428     nurbsbuf=1;
4429 
4430     /* save the original definition in a buffer */
4431     nurb.u_exp = nurbs[nurbsnr].u_exp;
4432     nurb.v_exp = nurbs[nurbsnr].v_exp;
4433     nurb.u_npnt= nurbs[nurbsnr].u_npnt;
4434     nurb.v_npnt= nurbs[nurbsnr].v_npnt;
4435     nurb.u_nknt= nurbs[nurbsnr].u_nknt;
4436     nurb.v_nknt= nurbs[nurbsnr].v_nknt;
4437     nurb.u_stride= nurbs[nurbsnr].u_stride;
4438     nurb.v_stride= nurbs[nurbsnr].v_stride;
4439     if ( (nurb.uknt = (GLfloat *)malloc( (nurbs[nurbsnr].u_nknt+1) * sizeof(GLfloat))) == NULL )
4440       printf("\n\n ERROR: realloc failed uknt\n\n");
4441     if ( (nurb.vknt = (GLfloat *)malloc( (nurbs[nurbsnr].v_nknt+1) * sizeof(GLfloat))) == NULL )
4442       printf("\n\n ERROR: realloc failed vknt\n\n");
4443     for(i=0; i<nurb.u_nknt; i++) { nurb.uknt[i]=nurbs[nurbsnr].uknt[i]; }
4444     for(i=0; i<nurb.v_nknt; i++) { nurb.vknt[i]=nurbs[nurbsnr].vknt[i]; }
4445     if( (nurb.ctlarray = (GLfloat *)malloc( (nurbs[nurbsnr].u_npnt*nurbs[nurbsnr].v_npnt*nurbs[nurbsnr].v_stride+5)*sizeof(GLfloat) )) == NULL )
4446 	  { printf(" ERROR: realloc failure in repairNurbs(), nurbs:%s can not be shaped\n\n", nurbs[nurbsnr].name); return(0); }
4447     for (i=0; i<nurbs[nurbsnr].u_npnt*nurbs[nurbsnr].v_npnt*nurbs[nurbsnr].v_stride+4; i++) nurb.ctlarray[i]=nurbs[nurbsnr].ctlarray[i];
4448 
4449     /* create the approximation (repairNurbs have to be called in a sem block) */
4450     if(nurbs[nurbsnr].u_exp>=gl_max_eval_order) repairNurbs( nurbsnr, nurbs[nurbsnr].u_exp-gl_max_eval_order+1, 0);
4451     if(nurbs[nurbsnr].v_exp>=gl_max_eval_order) repairNurbs( nurbsnr, nurbs[nurbsnr].v_exp-gl_max_eval_order+1, 1);
4452 
4453     if(printFlag) printf("nurbs generated and approximated:%s\n",nurbs[nurbsnr].name);
4454   }
4455   else nurbsbuf=0;
4456 
4457   glutSetWindow( w1);
4458 
4459   /* now the trimmed region is defined. Render the nurbs-patch into the back-buffer and store */
4460   /* the poligons inside the surface */
4461   patch=surf[nr].patch;
4462   if(printFlag) printf("patch:%d of NURBS:%s patches:%d is now rendered and stored in surf:%s\n", patch, nurbs[nurbsnr].name, nurbs[nurbsnr].patches, surf[nr].name );
4463 
4464   if(patch>=nurbs[nurbsnr].patches)
4465   {
4466     i=calcTrimLoops(nurbsnr, nr);
4467     if(i!=0) { surf[nr].npgn=0; if(i>0) surf[nr].fail=2; return(-1); }
4468   }
4469   printf(" glu rendering of surf:%s (should be avoided by changing the line divisions)\n",surf[nr].name);
4470   // the system libGLU has a bug and might be replaced by the https://archive.mesa3d.org/glu/glu-9.0.0.tar(SGI) used in Makefile_glu
4471 
4472   /* disable culling else not all surfs will be filled */
4473   glGetIntegerv( GL_CULL_FACE_MODE, ipuf );
4474   glDisable ( GL_CULL_FACE );
4475 
4476   /* malloc the feedbackbuffer, the address will be stored later and reallocated in surf[nr].pgn */
4477   size_fbb=GL_FEEDBACK_BUFF_SIZE;
4478   if( (feedbackbuffer= (GLfloat *)malloc(size_fbb*sizeof(GLfloat) )) == NULL )
4479   { printf(" ERROR: realloc failure, feedbackbuffer to big\n\n");
4480     return(0); }
4481   glLoadIdentity();
4482   glOrtho( -1.*aspectRatio_w1, 1.*aspectRatio_w1, -1., 1., -1, 1. );
4483   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
4484   glFeedbackBuffer (size_fbb, GL_3D, feedbackbuffer);
4485   glRenderMode (GL_FEEDBACK);
4486   /* draw the nurbs, copy from drawNurs_plot() */
4487   gluNurbsProperty(nurbs[nurbsnr].Nurb, GLU_DISPLAY_MODE, GLU_FILL);
4488   gluNurbsProperty(nurbs[nurbsnr].Nurb, GLU_CULLING, GL_TRUE);
4489   gluNurbsProperty(nurbs[nurbsnr].Nurb, GLU_SAMPLING_METHOD, GLU_DOMAIN_DISTANCE );
4490   gluNurbsProperty(nurbs[nurbsnr].Nurb, GLU_U_STEP,  nurbs[nurbsnr].ustep[patch] );
4491   gluNurbsProperty(nurbs[nurbsnr].Nurb, GLU_V_STEP,  nurbs[nurbsnr].vstep[patch] );
4492   gluBeginSurface(nurbs[nurbsnr].Nurb);
4493   gluNurbsSurface(nurbs[nurbsnr].Nurb,
4494   nurbs[nurbsnr].u_nknt, nurbs[nurbsnr].uknt,
4495   nurbs[nurbsnr].v_nknt, nurbs[nurbsnr].vknt,
4496   nurbs[nurbsnr].u_stride, nurbs[nurbsnr].v_stride,
4497   nurbs[nurbsnr].ctlarray,
4498   nurbs[nurbsnr].u_exp+1, nurbs[nurbsnr].v_exp+1,
4499   nurbs[nurbsnr].type);
4500   if(printFlag)
4501   {
4502     printf("nurbs-param: %d  %f %f  %d %d  %d %d  %d %d  %d  nod:%f %f %f %f arr:%f %f\n", size_fbb,
4503       nurbs[nurbsnr].ures,
4504       nurbs[nurbsnr].vres,
4505       nurbs[nurbsnr].u_nknt,
4506       nurbs[nurbsnr].v_nknt,
4507       nurbs[nurbsnr].u_stride, nurbs[nurbsnr].v_stride,
4508       nurbs[nurbsnr].u_exp+1, nurbs[nurbsnr].v_exp+1,
4509       nurbs[nurbsnr].type,
4510       nurbs[nurbsnr].uknt[0],
4511       nurbs[nurbsnr].uknt[nurbs[nurbsnr].u_nknt-1],
4512       nurbs[nurbsnr].vknt[0],
4513       nurbs[nurbsnr].vknt[nurbs[nurbsnr].v_nknt-1],
4514       nurbs[nurbsnr].ctlarray[0],
4515       nurbs[nurbsnr].ctlarray[nurbs[nurbsnr].u_npnt*nurbs[nurbsnr].v_npnt]
4516     );
4517   }
4518 
4519   for(i=0; i<nurbs[nurbsnr].nc[patch]; i++)
4520   {
4521     if(printFlag)
4522     {
4523       printf("glu input in cgx format u,v,- %d %d %d\n", nurbs[nurbsnr].trimFlag, nurbs[nurbsnr].nc[patch], i);
4524       n=0; for(j=0; j< nurbs[nurbsnr].np[patch][i]; j++)
4525       {
4526         printf(" pnt %d%d %e %e 0.\n",i,j, nurbs[nurbsnr].uv[patch][i][n], nurbs[nurbsnr].uv[patch][i][n+1]);
4527         n+=2;
4528         //printf(" pnt %d%d %lf %lf %lf\n",i,j, nurbs[nurbsnr].xyz[patch][i][n], nurbs[nurbsnr].xyz[patch][i][n+1], nurbs[nurbsnr].xyz[patch][i][n+2]);
4529         //n+=3;
4530       }
4531     }
4532     gluBeginTrim(nurbs[nurbsnr].Nurb);
4533     gluPwlCurve(nurbs[nurbsnr].Nurb, nurbs[nurbsnr].np[patch][i], nurbs[nurbsnr].uv[patch][i], (GLint)2, GLU_MAP1_TRIM_2);
4534     gluEndTrim(nurbs[nurbsnr].Nurb);
4535   }
4536 
4537   gluEndSurface(nurbs[nurbsnr].Nurb);
4538   surf[nr].npgn=glRenderMode (GL_RENDER);
4539 
4540   /* resore the original cull mode */
4541   if ( ipuf[0] == GL_BACK ) glCullFace ( GL_BACK );
4542   if ( ipuf[0] == GL_FRONT ) glCullFace ( GL_FRONT );
4543 
4544   if(size_fbb<surf[nr].npgn)
4545   {
4546     printf("ERROR in repSurf: feedbackbuffer:%d to small, increase at least to:%d \n", size_fbb, surf[nr].npgn);
4547     return(-1);
4548   }
4549 
4550   /* store data from (GLfloat *)feedbackbuffer into  (double *)surf[nr].pgn */
4551   if (surf[nr].npgn) free(surf[nr].pgn);
4552 
4553   /* get the address of a new array including the normals */
4554   surf[nr].npgn=adjustFeedBack( surf[nr].npgn, feedbackbuffer, &surf[nr].pgn);
4555   free(feedbackbuffer);
4556 
4557   /* the triangles must be inverted if the product of the nurbs-ori and surf-ori is '-' */
4558   if(surf[nr].sho=='-') oprod=-1; else oprod=1;
4559   if(surf[nr].ori=='-') oprod*=-1; else oprod*=1;
4560   if(oprod==-1)
4561   {
4562     n=0;
4563     while((surf[nr].npgn-n))
4564     {
4565       /* switch interior of surf, code from pickfunktions (l:2560) */
4566       n++; /* jump over the polygon token (ie.GL_POLYGON_TOKEN) */
4567       j=surf[nr].pgn[n++];
4568       surf[nr].pgn[n]*=-1;
4569       surf[nr].pgn[n+1]*=-1;
4570       surf[nr].pgn[n+2]*=-1;
4571       n+=3;
4572       if ((fbuf = (double *)malloc((j*3)*sizeof(double)) ) == NULL )
4573 		{ printf("\n\nERROR: realloc failure in flip\n\n"); return(0); }
4574       for(k=0; k<j; k++)
4575       {
4576         fbuf[j*3-k*3-3]=surf[nr].pgn[n];
4577         fbuf[j*3-k*3-2]=surf[nr].pgn[n+1];
4578         fbuf[j*3-k*3-1]=surf[nr].pgn[n+2];
4579         n+=3;
4580       }
4581       n-=3*j;
4582       for(k=0; k<j; k++)
4583       {
4584         surf[nr].pgn[n]  =fbuf[k*3] ;
4585         surf[nr].pgn[n+1]=fbuf[k*3+1];
4586         surf[nr].pgn[n+2]=fbuf[k*3+2];
4587         n+=3;
4588       }
4589       free(fbuf);
4590     }
4591   }
4592 
4593   /* correct the position of the trias if an nurbs-approximation was used and change back to the original nurbs */
4594   if(nurbsbuf)
4595   {
4596     /* restore the original definition */
4597     nurbs[nurbsnr].u_exp = nurb.u_exp;
4598     nurbs[nurbsnr].v_exp = nurb.v_exp;
4599     nurbs[nurbsnr].u_npnt= nurb.u_npnt;
4600     nurbs[nurbsnr].v_npnt= nurb.v_npnt;
4601     nurbs[nurbsnr].u_nknt= nurb.u_nknt;
4602     nurbs[nurbsnr].v_nknt= nurb.v_nknt;
4603     nurbs[nurbsnr].u_stride= nurb.u_stride;
4604     if ( (nurbs[nurbsnr].uknt = (GLfloat *)realloc(  (GLfloat *)nurbs[nurbsnr].uknt, (nurbs[nurbsnr].u_nknt+1) * sizeof(GLfloat))) == NULL )
4605       printf("\n\n ERROR: realloc failed uknt\n\n");
4606     if ( (nurbs[nurbsnr].vknt = (GLfloat *)realloc(  (GLfloat *)nurbs[nurbsnr].vknt, (nurbs[nurbsnr].v_nknt+1) * sizeof(GLfloat))) == NULL )
4607       printf("\n\n ERROR: realloc failed vknt\n\n");
4608     for(i=0; i<nurbs[nurbsnr].u_nknt; i++) { nurbs[nurbsnr].uknt[i]=nurb.uknt[i]; }
4609     for(i=0; i<nurbs[nurbsnr].v_nknt; i++) { nurbs[nurbsnr].vknt[i]=nurb.vknt[i]; }
4610     if( (nurbs[nurbsnr].ctlarray = (GLfloat *)realloc( (GLfloat *)nurbs[nurbsnr].ctlarray, (nurbs[nurbsnr].u_npnt*nurbs[nurbsnr].v_npnt*nurbs[nurbsnr].v_stride+5)*sizeof(GLfloat) )) == NULL )
4611 	    { printf(" ERROR: realloc failure in repairNurbs(), nurbs:%s can not be shaped\n\n", nurbs[nurbsnr].name); return(0); }
4612     for (i=0; i<nurbs[nurbsnr].u_npnt*nurbs[nurbsnr].v_npnt*4+4; i++) nurbs[nurbsnr].ctlarray[i]=nurb.ctlarray[i];
4613     free(nurb.ctlarray);
4614     free(nurb.uknt);
4615     free(nurb.vknt);
4616 
4617     /* project the vertexes to the original nurbs */
4618     if(surf[nr].fail!=2) projSurfToNurbs( nurbsnr, surf, nr, 0 );
4619   }
4620 
4621   return(1);
4622 }
4623 
4624 
4625 
4626 /*  store the polygons in the master-surface for illuminated rendering */
4627 /*  if the surface is nurbs-related: */
4628 /*  - create trimming-points in uv-space for dispaying and meshing trimmed Nurbs-Shapes */
4629 /*  if the surf is BLENDed */
4630 /*  - mesh the surface with tri3 */
4631 /* return 0 if not successfull, else !=0 */
repSurf(int nr,int renderFlag)4632 int repSurf(int nr, int renderFlag )
4633 {
4634   int i;
4635 
4636   int Stmp=-1, sh_buf=-1, nurbsnr=-1,returnVal=0;
4637 
4638   /* check if the surface is not meshable */
4639   if(surf[nr].o==NULL) return(0);
4640   if(surf[nr].o[0]==0) return(0);
4641 
4642   /* if its a BLENDed surface */
4643   if((renderFlag) && ((surf[nr].name != (char *)NULL )&&(surf[nr].sh<=-1)&&(surf[nr].nc>0)))
4644   {
4645     if(printFlag) printf("BLEND:%s\n",surf[nr].name);
4646     fillBlendedSurf(nr);
4647   }
4648 
4649   /* if its a NURBS related surface */
4650   if(( surf[nr].name != (char *)NULL )&&(surf[nr].sh>-1)&&(surf[nr].nc>0))
4651   {
4652     /* if shape, generate prelim nurbs */
4653     if(shape[surf[nr].sh].type>=0) Stmp= surfToNurs(nr);
4654     if(Stmp>-1)
4655     {
4656       sh_buf=surf[nr].sh;
4657     sem_wait(&sem_g);
4658       surf[nr].sh=shape_i( nurbs[Stmp].name, 4, Stmp, 0, 0, 0, 0,0,0);
4659     sem_post(&sem_g);
4660       if(surf[nr].pgn!=NULL)
4661       {
4662         free(surf[nr].pgn); surf[nr].pgn=NULL; surf[nr].npgn=0;
4663       }
4664       if(printFlag) printf (" interior changed to Nurbs: %s\n", nurbs[Stmp].name );
4665     }
4666 
4667     if(shape[surf[nr].sh].type==4)
4668     {
4669       nurbsnr=shape[surf[nr].sh].p[0];
4670 
4671       /* calculate the xyz and uv values and orientation of the trimming loops of the surface (patch) (might be included in orientSurf() later) */
4672       i=calcTrimLoops(nurbsnr, nr);
4673       if(i!=0)
4674       {
4675         if(i>0)
4676 	{
4677           printf(" ERROR: surf:%s could not be trimmed. All points are located on ambiguous edges. Try to fix the geometry manually.\n",surf[nr].name);
4678           surf[nr].fail=2;
4679 	}
4680         goto nurbsCouldNotBeTrimmed;
4681       }
4682 
4683       if(renderFlag)
4684       {
4685         returnVal=mesh_tr3u(nr, renderFlag);
4686         /* check if the surface could be trimmed and rendered */
4687         if(surf[nr].npgn<1)
4688         {
4689 	  if(printFlag) printf("WARNING: mesh_tr3u failed surf:%s \n",surf[nr].name);
4690           /* try to make a mapped mesh */
4691           /* change the element attribute to mapped mesh */
4692           surf[nr].eattr=0;
4693           fillBlendedSurf(nr);
4694           if(surf[nr].npgn>0) projSurfToNurbs( nurbsnr, surf, nr, 0 );
4695           else
4696 	  {
4697 	    if(printFlag) printf(" WARNING: surf:%s could not be rendered\n", surf[nr].name);
4698             surf[nr].eattr=-1;
4699             surf[nr].npgn=0;
4700 
4701           nurbsCouldNotBeTrimmed:;
4702             /* add surf to special set */
4703             seta(set_bsur, "s", nr );
4704             if(returnVal==-2) seta(set_glur, "s", nr);
4705 	  }
4706         }
4707       }
4708 
4709       /* restore the pointer to the shape */
4710       if(sh_buf>-1)
4711       {
4712         surf[nr].sh=sh_buf;
4713     sem_wait(&sem_g);
4714         for (i=0; i<nurbs[Stmp].u_npnt; i++) delPnt( nurbs[Stmp].v_npnt, nurbs[Stmp].ctlpnt[i] );
4715         delNurs( 1, &Stmp );
4716     sem_post(&sem_g);
4717       }
4718 
4719     }
4720   }
4721   if(surf[nr].fail==2) return(0);
4722   return(1);
4723 }
4724 
4725 
4726 
4727 /* create in-between-points in lines for dispaying curves */
4728 /* return 0 if failed */
repLine(int j)4729 int repLine(int j )
4730 {
4731   int k,n;
4732   double pn[3];
4733 
4734 
4735   /* calculation of the inner points of non-straight lines for drawing purposes */
4736   if( line[j].name != (char *)NULL )
4737   {
4738     if ((line[j].ip = (double *)realloc( (double *)line[j].ip, ((line[j].div+1)*3)*sizeof(double)) ) == NULL )
4739     { printf(" ERROR: realloc failure in rep(), Line:%s can not be shaped\n\n", line[j].name);
4740       return(0); }
4741 
4742     n=0;
4743     line[j].ip[n++]=point[line[j].p1].px;
4744     line[j].ip[n++]=point[line[j].p1].py;
4745     line[j].ip[n++]=point[line[j].p1].pz;
4746     if(line[j].typ=='a') for (k=0; k<line[j].div-1; k++)
4747     {
4748       arcNodes( j, k,line[j].div, pn );
4749       line[j].ip[n++]=pn[0];
4750       line[j].ip[n++]=pn[1];
4751       line[j].ip[n++]=pn[2];
4752     }
4753     else if (line[j].typ=='s') for (k=0; k<line[j].div-1; k++)
4754     {
4755       splineNodes( j, k,line[j].div, pn );
4756       line[j].ip[n++]=pn[0];
4757       line[j].ip[n++]=pn[1];
4758       line[j].ip[n++]=pn[2];
4759     }
4760     else if (line[j].typ=='n') for (k=0; k<line[j].div-1; k++)
4761     {
4762       nurlNodes( j, k,line[j].div, pn );
4763       line[j].ip[n++]=pn[0];
4764       line[j].ip[n++]=pn[1];
4765       line[j].ip[n++]=pn[2];
4766     }
4767     else for (k=0; k<line[j].div-1; k++)
4768     {
4769       /* must be! For the nurbs trimming all node-points are necessary */
4770       /* because long lines would not give enough inbetween points */
4771       straightNodes( j, k,line[j].div, pn );
4772       line[j].ip[n++]=pn[0];
4773       line[j].ip[n++]=pn[1];
4774       line[j].ip[n++]=pn[2];
4775     }
4776     line[j].ip[n++]=point[line[j].p2].px;
4777     line[j].ip[n++]=point[line[j].p2].py;
4778     line[j].ip[n++]=point[line[j].p2].pz;
4779     line[j].nip=n;
4780     /*
4781   printf("line[%d].name:%s line[%d].typ:%c line[%d].div:%d line[%d].nip:%d\n"
4782   , j,line[j].name,j,line[j].typ,j,line[j].div,j, line[j].nip);
4783     */
4784   }
4785   return(1);
4786 }
4787 
4788 
4789 
4790 /* changes linear elem to quadratic formulation or vice versa and generates nodes in the mid of the face for drawing purposes only */
4791 /* w/o parameter it adjusts the midside nodes of quadratic formulated elements */
fixMidsideNodes(char * setname,char * parameter)4792 void fixMidsideNodes( char *setname, char *parameter)
4793 {
4794   int i,j,n,f,k,n1,n2,nm, nnew[20], nf;
4795   int nodseq_tr3[]={0,1,1,2,2,0};
4796   int nodseq_tr6[]={0,3,1,1,4,2,2,5,0};
4797   int nodseq_qu4[]={0,1,1,2,2,3,3,0};
4798   int nodseq_qu8[]={0,4,1,1,5,2,2,6,3,3,7,0};
4799   int nodseq_te4[]={0,1,1,2,2,0, 0,3,1,3,2,3};
4800   int nodseq_pe6[]={0,1,1,2,2,0, 3,4,4,5,5,3, 0,3,1,4,2,5};
4801   int nodseq_he8[]={0,1,1,2,2,3,3,0, 4,5,5,6,6,7,7,4, 0,4,1,5,2,6,3,7};
4802   int nodseq_te10[]={0,4,1,1,5,2,2,6,0, 0,7,3,1,8,3,2,9,3};
4803   int nodseq_pe15[]={0,6,1,1,7,2,2,8,0, 3,12,4,4,13,5,5,14,3, 0,9,3,1,10,4,2,11,5};
4804   int nodseq_he20[]={0,8,1,1,9,2,2,10,3,3,11,0, 4,16,5,5,17,6,6,18,7,7,19,4, 0,12,4,1,13,5,2,14,6,3,15,7};
4805 
4806   int midnod_tr6[]={3,4,5};
4807   int midnod_qu8[]={4,5,6,7};
4808   int midnod_pe15[]={6,7,8,9,10,11,12,13,14};
4809   int midnod_te10[]={4,5,6,7,8,9};
4810   int midnod_he20[]={8,9,10,11,12,13,14,15,16,17,18,19};
4811 
4812   int setNr, mode=0, *facenod=NULL,ipuf=0, anz_nmax;
4813 
4814   typedef struct {
4815     int sum, *n2, *nm;
4816   }N1nm;
4817   N1nm *n1nm;
4818 
4819   setNr=getSetNr(setname);
4820 
4821   if (setNr<0)
4822   {
4823     printf (" delSet: set:%s does not exist\n", setname);
4824     return;
4825   }
4826   if (set[setNr].anz_e==0)
4827   {
4828     printf (" delSet: set:%s contains no elements\n", setname);
4829     return;
4830   }
4831 
4832   /* remove midside nodes */
4833   if(compare(parameter,"rem",2)==2)
4834   {
4835     printf("store midside nodes from set:%s in set -delete for further manual manipulation\n",set[setNr].name);
4836     j=pre_seta("-delete", "i", 0 );
4837     for(k=0; k<set[setNr].anz_e; k++)
4838     {
4839       /* go over the dep elem and delete the midside nodes */
4840       for (k=0; k<set[setNr].anz_e; k++)
4841       {
4842         if (e_enqire[set[setNr].elem[k]].type == 4)
4843         {
4844           for (n=0; n<12; n++) seta( j, "n", e_enqire[set[setNr].elem[k]].nod[midnod_he20[n]] );
4845           /* change element def. */
4846           e_enqire[set[setNr].elem[k]].type =1;
4847         }
4848         else if (e_enqire[set[setNr].elem[k]].type == 5)
4849         {
4850           for (n=0; n<9; n++) seta( j, "n", e_enqire[set[setNr].elem[k]].nod[midnod_pe15[n]] );
4851           /* change element def. */
4852           e_enqire[set[setNr].elem[k]].type =2;
4853         }
4854         else if (e_enqire[set[setNr].elem[k]].type == 6)
4855         {
4856           //for (n=0; n<6; n++) printf("n:%d\n", e_enqire[set[setNr].elem[k]].nod[midnod_te10[n]] );
4857           for (n=0; n<6; n++) seta( j, "n", e_enqire[set[setNr].elem[k]].nod[midnod_te10[n]] );
4858           /* change element def. */
4859           e_enqire[set[setNr].elem[k]].type =3;
4860         }
4861         else if (e_enqire[set[setNr].elem[k]].type == 8)
4862         {
4863           for (n=0; n<3; n++) seta( j, "n", e_enqire[set[setNr].elem[k]].nod[midnod_tr6[n]] );
4864           /* change element def. */
4865           e_enqire[set[setNr].elem[k]].type =7;
4866         }
4867         else if (e_enqire[set[setNr].elem[k]].type == 10)
4868         {
4869           for (n=0; n<4; n++) seta( j, "n", e_enqire[set[setNr].elem[k]].nod[midnod_qu8[n]] );
4870           /* change element def. */
4871           e_enqire[set[setNr].elem[k]].type =9;
4872         }
4873       }
4874     }
4875   }
4876 
4877   /* generate new midside nodes for linear elements (qu4) */
4878   else if(compare(parameter,"gen",2)==2)
4879   {
4880     /* create a table for all nodes which points to already created midside nodes */
4881     if ( (n1nm = (N1nm *)malloc( (anz->nmax+1) * sizeof(N1nm))) == NULL )
4882     { printf("\n\n ERROR in mids: malloc\n\n") ; exit(-1); }
4883     for (i=0; i<=anz->nmax; i++) n1nm[i].sum=0;
4884     for (i=0; i<=anz->nmax; i++) n1nm[i].n2=n1nm[i].nm=NULL;
4885     anz_nmax=anz->nmax;
4886 
4887     /* go over the dep elem and corr midside nodes */
4888     for (k=0; k<set[setNr].anz_e; k++)
4889     {
4890       /* free space for the normal-vectors */
4891       if (e_enqire[set[setNr].elem[k]].type == 1)       nf=6;  /* HEXA8 */
4892       else if (e_enqire[set[setNr].elem[k]].type == 2)  nf=6;  /* PENTA6 */
4893       else if (e_enqire[set[setNr].elem[k]].type == 3)  nf=4;  /* TET4 */
4894       else if (e_enqire[set[setNr].elem[k]].type == 7)  nf=1;  /* TRI3  */
4895       else if (e_enqire[set[setNr].elem[k]].type == 9)  nf=2; /* QUAD4 */
4896       else if (e_enqire[set[setNr].elem[k]].type == 11) nf=1; /* BEAM */
4897       else nf=0;
4898       for(i=0; i<nf; i++) free(e_enqire[set[setNr].elem[k]].side[i]);
4899 
4900       if (e_enqire[set[setNr].elem[k]].type == 1)
4901       {
4902         for (n=0; n<12; n++)
4903         {
4904           nnew[nodseq_he20[n*3]]=  n1=e_enqire[set[setNr].elem[k]].nod[nodseq_he8[n*2]];
4905           nnew[nodseq_he20[n*3+2]]=  n2=e_enqire[set[setNr].elem[k]].nod[nodseq_he8[n*2+1]];
4906 
4907           /* check if the nm exists already */
4908           nm=-1;
4909           for(i=0; i<n1nm[n1].sum; i++) if(n1nm[n1].n2[i]==n2) nm=n1nm[n1].nm[i];
4910           for(i=0; i<n1nm[n2].sum; i++) if(n1nm[n2].n2[i]==n1) nm=n1nm[n2].nm[i];
4911 
4912           if(nm==-1)
4913 	  {
4914             /* generate new node */
4915             nm=nod( anz, &node, 1, anz->nnext++, 0., 0., 0., 0 );
4916 
4917             if ( (n1nm[n1].n2 = (int *)realloc( n1nm[n1].n2, (n1nm[n1].sum+1) * sizeof(int))) == NULL )
4918             { printf("\n\n ERROR in mids: realloc\n\n") ; exit(-1); }
4919             if ( (n1nm[n1].nm = (int *)realloc( n1nm[n1].nm, (n1nm[n1].sum+1) * sizeof(int))) == NULL )
4920             { printf("\n\n ERROR in mids: realloc\n\n") ; exit(-1); }
4921             n1nm[n1].n2[n1nm[n1].sum]=n2;
4922             n1nm[n1].nm[n1nm[n1].sum]=nm;
4923             n1nm[n1].sum++;
4924             adjustMidsideNode( &node[n1].nx, &node[n2].nx, &node[node[nm].nr].nx, 1);
4925 	  }
4926 
4927           nnew[nodseq_he20[n*3+1]]=node[nm].nr;
4928         }
4929         /* change element def. */
4930         e_enqire[set[setNr].elem[k]].type =4;
4931         for (n=0; n<20; n++) e_enqire[set[setNr].elem[k]].nod[n]=nnew[n];
4932       }
4933 
4934       else if (e_enqire[set[setNr].elem[k]].type == 2)
4935       {
4936         for (n=0; n<9; n++)
4937         {
4938           nnew[nodseq_pe15[n*3]]=  n1=e_enqire[set[setNr].elem[k]].nod[nodseq_pe6[n*2]];
4939           nnew[nodseq_pe15[n*3+2]]=  n2=e_enqire[set[setNr].elem[k]].nod[nodseq_pe6[n*2+1]];
4940 
4941           /* check if the nm exists already */
4942           nm=-1;
4943           for(i=0; i<n1nm[n1].sum; i++) if(n1nm[n1].n2[i]==n2) nm=n1nm[n1].nm[i];
4944           for(i=0; i<n1nm[n2].sum; i++) if(n1nm[n2].n2[i]==n1) nm=n1nm[n2].nm[i];
4945 
4946           if(nm==-1)
4947 	  {
4948             /* generate new node */
4949             nm=nod( anz, &node, 1, anz->nnext++, 0., 0., 0., 0 );
4950             if ( (n1nm[n1].n2 = (int *)realloc( n1nm[n1].n2, (n1nm[n1].sum+1) * sizeof(int))) == NULL )
4951             { printf("\n\n ERROR in mids: realloc\n\n") ; exit(-1); }
4952             if ( (n1nm[n1].nm = (int *)realloc( n1nm[n1].nm, (n1nm[n1].sum+1) * sizeof(int))) == NULL )
4953             { printf("\n\n ERROR in mids: realloc\n\n") ; exit(-1); }
4954             n1nm[n1].n2[n1nm[n1].sum]=n2;
4955             n1nm[n1].nm[n1nm[n1].sum]=nm;
4956             n1nm[n1].sum++;
4957             adjustMidsideNode( &node[n1].nx, &node[n2].nx, &node[node[nm].nr].nx, 1);
4958 	  }
4959 
4960           nnew[nodseq_pe15[n*3+1]]=node[nm].nr;
4961         }
4962         /* change element def. */
4963         e_enqire[set[setNr].elem[k]].type =5;
4964         for (n=0; n<15; n++) e_enqire[set[setNr].elem[k]].nod[n]=nnew[n];
4965       }
4966 
4967 
4968       else if (e_enqire[set[setNr].elem[k]].type == 3)
4969       {
4970         for (n=0; n<6; n++)
4971         {
4972           nnew[nodseq_te10[n*3]]=  n1=e_enqire[set[setNr].elem[k]].nod[nodseq_te4[n*2]];
4973           nnew[nodseq_te10[n*3+2]]=  n2=e_enqire[set[setNr].elem[k]].nod[nodseq_te4[n*2+1]];
4974 
4975           /* check if the nm exists already */
4976           nm=-1;
4977           for(i=0; i<n1nm[n1].sum; i++) if(n1nm[n1].n2[i]==n2) nm=n1nm[n1].nm[i];
4978           for(i=0; i<n1nm[n2].sum; i++) if(n1nm[n2].n2[i]==n1) nm=n1nm[n2].nm[i];
4979 
4980           if(nm==-1)
4981 	  {
4982             /* generate new node */
4983             nm=nod( anz, &node, 1, anz->nnext++, 0., 0., 0., 0 );
4984 
4985             if ( (n1nm[n1].n2 = (int *)realloc( n1nm[n1].n2, (n1nm[n1].sum+1) * sizeof(int))) == NULL )
4986             { printf("\n\n ERROR in mids: realloc\n\n") ; exit(-1); }
4987             if ( (n1nm[n1].nm = (int *)realloc( n1nm[n1].nm, (n1nm[n1].sum+1) * sizeof(int))) == NULL )
4988             { printf("\n\n ERROR in mids: realloc\n\n") ; exit(-1); }
4989             n1nm[n1].n2[n1nm[n1].sum]=n2;
4990             n1nm[n1].nm[n1nm[n1].sum]=nm;
4991             n1nm[n1].sum++;
4992             adjustMidsideNode( &node[n1].nx, &node[n2].nx, &node[node[nm].nr].nx, 1);
4993 	  }
4994           nnew[nodseq_te10[n*3+1]]=node[nm].nr;
4995         }
4996         /* change element def. */
4997         e_enqire[set[setNr].elem[k]].type =6;
4998         for (n=0; n<10; n++) e_enqire[set[setNr].elem[k]].nod[n]=nnew[n];
4999       }
5000 
5001       else if (e_enqire[set[setNr].elem[k]].type == 7)
5002       {
5003         for (n=0; n<3; n++)
5004         {
5005           nnew[nodseq_tr6[n*3]]=  n1=e_enqire[set[setNr].elem[k]].nod[nodseq_tr3[n*2]];
5006           nnew[nodseq_tr6[n*3+2]]=  n2=e_enqire[set[setNr].elem[k]].nod[nodseq_tr3[n*2+1]];
5007 
5008           /* check if the nm exists already */
5009           nm=-1;
5010           for(i=0; i<n1nm[n1].sum; i++) if(n1nm[n1].n2[i]==n2) nm=n1nm[n1].nm[i];
5011           for(i=0; i<n1nm[n2].sum; i++) if(n1nm[n2].n2[i]==n1) nm=n1nm[n2].nm[i];
5012 
5013           if(nm==-1)
5014 	  {
5015             /* generate new node */
5016             nm=nod( anz, &node, 1, anz->nnext++, 0., 0., 0., 0 );
5017 
5018             if ( (n1nm[n1].n2 = (int *)realloc( n1nm[n1].n2, (n1nm[n1].sum+1) * sizeof(int))) == NULL )
5019             { printf("\n\n ERROR in mids: realloc\n\n") ; exit(-1); }
5020             if ( (n1nm[n1].nm = (int *)realloc( n1nm[n1].nm, (n1nm[n1].sum+1) * sizeof(int))) == NULL )
5021             { printf("\n\n ERROR in mids: realloc\n\n") ; exit(-1); }
5022             n1nm[n1].n2[n1nm[n1].sum]=n2;
5023             n1nm[n1].nm[n1nm[n1].sum]=nm;
5024             n1nm[n1].sum++;
5025             adjustMidsideNode( &node[n1].nx, &node[n2].nx, &node[node[nm].nr].nx, 1);
5026 	  }
5027 
5028           nnew[nodseq_tr6[n*3+1]]=node[nm].nr;
5029         }
5030         /* change element def. */
5031         e_enqire[set[setNr].elem[k]].type =8;
5032         for (n=0; n<6; n++) e_enqire[set[setNr].elem[k]].nod[n]=nnew[n];
5033       }
5034 
5035       else if (e_enqire[set[setNr].elem[k]].type == 9)
5036       {
5037         for (n=0; n<4; n++)
5038         {
5039           nnew[nodseq_qu8[n*3]]=  n1=e_enqire[set[setNr].elem[k]].nod[nodseq_qu4[n*2]];
5040           nnew[nodseq_qu8[n*3+2]]=  n2=e_enqire[set[setNr].elem[k]].nod[nodseq_qu4[n*2+1]];
5041 
5042           /* check if the nm exists already */
5043           nm=-1;
5044           for(i=0; i<n1nm[n1].sum; i++) if(n1nm[n1].n2[i]==n2) nm=n1nm[n1].nm[i];
5045           for(i=0; i<n1nm[n2].sum; i++) if(n1nm[n2].n2[i]==n1) nm=n1nm[n2].nm[i];
5046 
5047           if(nm==-1)
5048 	  {
5049             /* generate new node */
5050             nm=nod( anz, &node, 1, anz->nnext++, 0., 0., 0., 0 );
5051 
5052             if ( (n1nm[n1].n2 = (int *)realloc( n1nm[n1].n2, (n1nm[n1].sum+1) * sizeof(int))) == NULL )
5053             { printf("\n\n ERROR in mids: realloc\n\n") ; exit(-1); }
5054             if ( (n1nm[n1].nm = (int *)realloc( n1nm[n1].nm, (n1nm[n1].sum+1) * sizeof(int))) == NULL )
5055             { printf("\n\n ERROR in mids: realloc\n\n") ; exit(-1); }
5056             n1nm[n1].n2[n1nm[n1].sum]=n2;
5057             n1nm[n1].nm[n1nm[n1].sum]=nm;
5058             n1nm[n1].sum++;
5059             adjustMidsideNode( &node[n1].nx, &node[n2].nx, &node[node[nm].nr].nx, 1);
5060 	  }
5061 
5062           nnew[nodseq_qu8[n*3+1]]=node[nm].nr;
5063         }
5064         /* change element def. */
5065         e_enqire[set[setNr].elem[k]].type =10;
5066         for (n=0; n<8; n++) e_enqire[set[setNr].elem[k]].nod[n]=nnew[n];
5067       }
5068 
5069       /* space for the normal-vectors */
5070       if (e_enqire[set[setNr].elem[k]].type == 1)       nf=6;  /* HEXA8 */
5071       else if (e_enqire[set[setNr].elem[k]].type == 2)  nf=6;  /* PENTA6 */
5072       else if (e_enqire[set[setNr].elem[k]].type == 3)  nf=4;  /* TET4 */
5073       else if (e_enqire[set[setNr].elem[k]].type == 4)  nf=48; /* HEXA20 */
5074       else if (e_enqire[set[setNr].elem[k]].type == 5)  nf=48; /* PENTA15 */
5075       else if (e_enqire[set[setNr].elem[k]].type == 6)  nf=16; /* TET10 */
5076       else if (e_enqire[set[setNr].elem[k]].type == 7)  nf=1;  /* TRI3  */
5077       else if (e_enqire[set[setNr].elem[k]].type == 8)  nf=4; /* TRI6  */
5078       else if (e_enqire[set[setNr].elem[k]].type == 9)  nf=2; /* QUAD4 */
5079       else if (e_enqire[set[setNr].elem[k]].type == 10) nf=8; /* QUAD8 */
5080       else if (e_enqire[set[setNr].elem[k]].type == 11) nf=1; /* BEAM */
5081       else if (e_enqire[set[setNr].elem[k]].type == 12) nf=1; /* BEAM3 */
5082 
5083       if((e_enqire[set[setNr].elem[k]].side=(double **)realloc(e_enqire[set[setNr].elem[k]].side, (nf)*sizeof(double *)))==NULL)
5084         printf("\n\n ERROR: realloc failed\n\n" );
5085       for(i=0; i<nf; i++)
5086       {
5087         if((e_enqire[set[setNr].elem[k]].side[i]=(double *)malloc((3)*sizeof(double)))==NULL)
5088           printf("\n\n ERROR: malloc failed\n\n" );
5089       }
5090     }
5091     for (i=0; i<=anz_nmax; i++) { free(n1nm[i].n2); free(n1nm[i].nm); }
5092     free(n1nm);
5093   }
5094 
5095   /* adjust midside nodes */
5096   else
5097   {
5098     /* midside nodes on faces are not lineary readjusted. They stay on the orig curvature. */
5099     if(compare(parameter,"lin",2)==2)
5100     {
5101       mode=1;
5102       if( (facenod=(int *)calloc( (anz->nmax+1),sizeof(int) ) )==NULL)
5103       { printf(" ERROR: realloc failure in fixMidsideNodes\n\n" ); return; }
5104       for (f=0; f<anz->nmax+1; f++) facenod[f]=0;
5105       for (f=0; f<anz->f; f++)
5106       {
5107         if (face[f].type == 7) ipuf = 3;  /* TRI3 */
5108         else if (face[f].type== 8) ipuf = 6;  /* TRI6  */
5109         else if (face[f].type == 9) ipuf = 4;  /* QU4 */
5110         else if (face[f].type == 10) ipuf = 8;  /* QU8 */
5111         else ipuf=0;
5112 
5113         for( i=0; i<ipuf; i++)
5114         {
5115           facenod[face[f].nod[i]]=1;
5116         }
5117       }
5118     }
5119     for(k=0; k<set[setNr].anz_e; k++)
5120     {
5121       /* go over the dep elem and corr midside nodes */
5122       for (k=0; k<set[setNr].anz_e; k++)
5123       {
5124         if      (e_enqire[set[setNr].elem[k]].type == 4)
5125         {
5126           for (n=0; n<12; n++)
5127           {
5128             n1=e_enqire[set[setNr].elem[k]].nod[nodseq_he20[n*3]];
5129             nm=e_enqire[set[setNr].elem[k]].nod[nodseq_he20[n*3+1]];
5130             n2=e_enqire[set[setNr].elem[k]].nod[nodseq_he20[n*3+2]];
5131             if((mode)&&(facenod[nm]==1)) adjustMidsideNode( &node[n1].nx, &node[n2].nx, &node[nm].nx, 0);
5132             else adjustMidsideNode( &node[n1].nx, &node[n2].nx, &node[nm].nx, mode);
5133           }
5134         }
5135         else if (e_enqire[set[setNr].elem[k]].type == 5)
5136         {
5137           for (n=0; n<9; n++)
5138           {
5139             n1=e_enqire[set[setNr].elem[k]].nod[nodseq_pe15[n*3]];
5140             nm=e_enqire[set[setNr].elem[k]].nod[nodseq_pe15[n*3+1]];
5141             n2=e_enqire[set[setNr].elem[k]].nod[nodseq_pe15[n*3+2]];
5142             if((mode)&&(facenod[nm]==1)) adjustMidsideNode( &node[n1].nx, &node[n2].nx, &node[nm].nx, 0);
5143             adjustMidsideNode( &node[n1].nx, &node[n2].nx, &node[nm].nx, mode);
5144           }
5145         }
5146         else if (e_enqire[set[setNr].elem[k]].type == 6)
5147         {
5148           for (n=0; n<6; n++)
5149           {
5150             n1=e_enqire[set[setNr].elem[k]].nod[nodseq_te10[n*3]];
5151             nm=e_enqire[set[setNr].elem[k]].nod[nodseq_te10[n*3+1]];
5152             n2=e_enqire[set[setNr].elem[k]].nod[nodseq_te10[n*3+2]];
5153             if((mode)&&(facenod[nm]==1)) adjustMidsideNode( &node[n1].nx, &node[n2].nx, &node[nm].nx, 0);
5154             adjustMidsideNode( &node[n1].nx, &node[n2].nx, &node[nm].nx, mode);
5155           }
5156         }
5157       }
5158     }
5159   }
5160   free(facenod);
5161 }
5162 
5163 
5164 
posMidsideNodes(Nodes * node)5165 void posMidsideNodes(Nodes *node)
5166 {
5167   int i, n, n1,n2;
5168 
5169       for ( i=0; i<anz->e; i++ )
5170       {
5171         if(e_enqire[e_enqire[i].nr].type==4)
5172         {
5173           for (n=0; n<3; n++)
5174           {
5175           node[e_enqire[e_enqire[i].nr].nod[20+n]].nx =-0.25* (
5176           node[e_enqire[e_enqire[i].nr].nod[0+n]].nx+node[e_enqire[e_enqire[i].nr].nod[1+n]].nx    +
5177           node[e_enqire[e_enqire[i].nr].nod[5+n]].nx+node[e_enqire[e_enqire[i].nr].nod[4+n]].nx )  + 0.5*(
5178           node[e_enqire[e_enqire[i].nr].nod[8+n]].nx+node[e_enqire[e_enqire[i].nr].nod[13+n]].nx   +
5179           node[e_enqire[e_enqire[i].nr].nod[16+n]].nx+node[e_enqire[e_enqire[i].nr].nod[12+n]].nx) ;
5180 
5181           node[e_enqire[e_enqire[i].nr].nod[20+n]].ny =-0.25* (
5182           node[e_enqire[e_enqire[i].nr].nod[0+n]].ny+node[e_enqire[e_enqire[i].nr].nod[1+n]].ny    +
5183           node[e_enqire[e_enqire[i].nr].nod[5+n]].ny+node[e_enqire[e_enqire[i].nr].nod[4+n]].ny )  + 0.5*(
5184           node[e_enqire[e_enqire[i].nr].nod[8+n]].ny+node[e_enqire[e_enqire[i].nr].nod[13+n]].ny   +
5185           node[e_enqire[e_enqire[i].nr].nod[16+n]].ny+node[e_enqire[e_enqire[i].nr].nod[12+n]].ny) ;
5186 
5187           node[e_enqire[e_enqire[i].nr].nod[20+n]].nz =-0.25* (
5188           node[e_enqire[e_enqire[i].nr].nod[0+n]].nz+node[e_enqire[e_enqire[i].nr].nod[1+n]].nz    +
5189           node[e_enqire[e_enqire[i].nr].nod[5+n]].nz+node[e_enqire[e_enqire[i].nr].nod[4+n]].nz )  + 0.5*(
5190           node[e_enqire[e_enqire[i].nr].nod[8+n]].nz+node[e_enqire[e_enqire[i].nr].nod[13+n]].nz   +
5191           node[e_enqire[e_enqire[i].nr].nod[16+n]].nz+node[e_enqire[e_enqire[i].nr].nod[12+n]].nz) ;
5192           }
5193           node[e_enqire[e_enqire[i].nr].nod[23  ]].nx =-0.25* (
5194           node[e_enqire[e_enqire[i].nr].nod[3]].nx+node[e_enqire[e_enqire[i].nr].nod[0]].nx    +
5195           node[e_enqire[e_enqire[i].nr].nod[4]].nx+node[e_enqire[e_enqire[i].nr].nod[7]].nx )  + 0.5*(
5196           node[e_enqire[e_enqire[i].nr].nod[11]].nx+node[e_enqire[e_enqire[i].nr].nod[12]].nx   +
5197           node[e_enqire[e_enqire[i].nr].nod[19]].nx+node[e_enqire[e_enqire[i].nr].nod[15]].nx) ;
5198 
5199           node[e_enqire[e_enqire[i].nr].nod[23  ]].ny =-0.25* (
5200           node[e_enqire[e_enqire[i].nr].nod[3]].ny+node[e_enqire[e_enqire[i].nr].nod[0]].ny    +
5201           node[e_enqire[e_enqire[i].nr].nod[4]].ny+node[e_enqire[e_enqire[i].nr].nod[7]].ny )  + 0.5*(
5202           node[e_enqire[e_enqire[i].nr].nod[11]].ny+node[e_enqire[e_enqire[i].nr].nod[12]].ny   +
5203           node[e_enqire[e_enqire[i].nr].nod[19]].ny+node[e_enqire[e_enqire[i].nr].nod[15]].ny) ;
5204 
5205           node[e_enqire[e_enqire[i].nr].nod[23  ]].nz =-0.25* (
5206           node[e_enqire[e_enqire[i].nr].nod[3]].nz+node[e_enqire[e_enqire[i].nr].nod[0]].nz    +
5207           node[e_enqire[e_enqire[i].nr].nod[4]].nz+node[e_enqire[e_enqire[i].nr].nod[7]].nz )  + 0.5*(
5208           node[e_enqire[e_enqire[i].nr].nod[11]].nz+node[e_enqire[e_enqire[i].nr].nod[12]].nz   +
5209           node[e_enqire[e_enqire[i].nr].nod[19]].nz+node[e_enqire[e_enqire[i].nr].nod[15]].nz) ;
5210           for (n=0; n<2; n++)
5211           {
5212           n1=n*4;
5213           n2=n*8;
5214           node[e_enqire[e_enqire[i].nr].nod[24+n  ]].nx =-0.25* (
5215           node[e_enqire[e_enqire[i].nr].nod[0+n1]].nx+node[e_enqire[e_enqire[i].nr].nod[1+n1]].nx    +
5216           node[e_enqire[e_enqire[i].nr].nod[2+n1]].nx+node[e_enqire[e_enqire[i].nr].nod[3+n1]].nx )  + 0.5*(
5217           node[e_enqire[e_enqire[i].nr].nod[8+n2]].nx+node[e_enqire[e_enqire[i].nr].nod[9+n2]].nx   +
5218           node[e_enqire[e_enqire[i].nr].nod[10+n2]].nx+node[e_enqire[e_enqire[i].nr].nod[11+n2]].nx) ;
5219 
5220           node[e_enqire[e_enqire[i].nr].nod[24+n  ]].ny =-0.25* (
5221           node[e_enqire[e_enqire[i].nr].nod[0+n1]].ny+node[e_enqire[e_enqire[i].nr].nod[1+n1]].ny    +
5222           node[e_enqire[e_enqire[i].nr].nod[2+n1]].ny+node[e_enqire[e_enqire[i].nr].nod[3+n1]].ny )  + 0.5*(
5223           node[e_enqire[e_enqire[i].nr].nod[8+n2]].ny+node[e_enqire[e_enqire[i].nr].nod[9+n2]].ny   +
5224           node[e_enqire[e_enqire[i].nr].nod[10+n2]].ny+node[e_enqire[e_enqire[i].nr].nod[11+n2]].ny) ;
5225 
5226           node[e_enqire[e_enqire[i].nr].nod[24+n  ]].nz =-0.25* (
5227           node[e_enqire[e_enqire[i].nr].nod[0+n1]].nz+node[e_enqire[e_enqire[i].nr].nod[1+n1]].nz    +
5228           node[e_enqire[e_enqire[i].nr].nod[2+n1]].nz+node[e_enqire[e_enqire[i].nr].nod[3+n1]].nz )  + 0.5*(
5229           node[e_enqire[e_enqire[i].nr].nod[8+n2]].nz+node[e_enqire[e_enqire[i].nr].nod[9+n2]].nz   +
5230           node[e_enqire[e_enqire[i].nr].nod[10+n2]].nz+node[e_enqire[e_enqire[i].nr].nod[11+n2]].nz) ;
5231           }
5232         }
5233         if(e_enqire[e_enqire[i].nr].type==5)
5234         {
5235           for (n=0; n<2; n++)  /* create new nodes in center of areas */
5236           {
5237           node[e_enqire[e_enqire[i].nr].nod[15+n]].nx = -0.25* (
5238           node[e_enqire[e_enqire[i].nr].nod[0+n]].nx+node[e_enqire[e_enqire[i].nr].nod[1+n]].nx    +
5239           node[e_enqire[e_enqire[i].nr].nod[4+n]].nx+node[e_enqire[e_enqire[i].nr].nod[3+n]].nx )  + 0.5*(
5240           node[e_enqire[e_enqire[i].nr].nod[6+n]].nx+node[e_enqire[e_enqire[i].nr].nod[10+n]].nx   +
5241           node[e_enqire[e_enqire[i].nr].nod[12+n]].nx+node[e_enqire[e_enqire[i].nr].nod[ 9+n]].nx) ;
5242 
5243           node[e_enqire[e_enqire[i].nr].nod[15+n]].ny = -0.25* (
5244           node[e_enqire[e_enqire[i].nr].nod[0+n]].ny+node[e_enqire[e_enqire[i].nr].nod[1+n]].ny    +
5245           node[e_enqire[e_enqire[i].nr].nod[4+n]].ny+node[e_enqire[e_enqire[i].nr].nod[3+n]].ny )  + 0.5*(
5246           node[e_enqire[e_enqire[i].nr].nod[6+n]].ny+node[e_enqire[e_enqire[i].nr].nod[10+n]].ny   +
5247           node[e_enqire[e_enqire[i].nr].nod[12+n]].ny+node[e_enqire[e_enqire[i].nr].nod[ 9+n]].ny) ;
5248 
5249           node[e_enqire[e_enqire[i].nr].nod[15+n]].nz = -0.25* (
5250           node[e_enqire[e_enqire[i].nr].nod[0+n]].nz+node[e_enqire[e_enqire[i].nr].nod[1+n]].nz    +
5251           node[e_enqire[e_enqire[i].nr].nod[4+n]].nz+node[e_enqire[e_enqire[i].nr].nod[3+n]].nz )  + 0.5*(
5252           node[e_enqire[e_enqire[i].nr].nod[6+n]].nz+node[e_enqire[e_enqire[i].nr].nod[10+n]].nz   +
5253           node[e_enqire[e_enqire[i].nr].nod[12+n]].nz+node[e_enqire[e_enqire[i].nr].nod[ 9+n]].nz) ;
5254           }
5255 
5256           /* create  new node in center of area3 */
5257           node[e_enqire[e_enqire[i].nr].nod[17  ]].nx = -0.25* (
5258           node[e_enqire[e_enqire[i].nr].nod[2]].nx+node[e_enqire[e_enqire[i].nr].nod[0]].nx    +
5259           node[e_enqire[e_enqire[i].nr].nod[3]].nx+node[e_enqire[e_enqire[i].nr].nod[5]].nx )  + 0.5*(
5260           node[e_enqire[e_enqire[i].nr].nod[ 8]].nx+node[e_enqire[e_enqire[i].nr].nod[ 9]].nx   +
5261           node[e_enqire[e_enqire[i].nr].nod[14]].nx+node[e_enqire[e_enqire[i].nr].nod[11]].nx) ;
5262 
5263           node[e_enqire[e_enqire[i].nr].nod[17  ]].ny = -0.25* (
5264           node[e_enqire[e_enqire[i].nr].nod[2]].ny+node[e_enqire[e_enqire[i].nr].nod[0]].ny    +
5265           node[e_enqire[e_enqire[i].nr].nod[3]].ny+node[e_enqire[e_enqire[i].nr].nod[5]].ny )  + 0.5*(
5266           node[e_enqire[e_enqire[i].nr].nod[ 8]].ny+node[e_enqire[e_enqire[i].nr].nod[ 9]].ny   +
5267           node[e_enqire[e_enqire[i].nr].nod[14]].ny+node[e_enqire[e_enqire[i].nr].nod[11]].ny) ;
5268 
5269           node[e_enqire[e_enqire[i].nr].nod[17  ]].nz = -0.25* (
5270           node[e_enqire[e_enqire[i].nr].nod[2]].nz+node[e_enqire[e_enqire[i].nr].nod[0]].nz    +
5271           node[e_enqire[e_enqire[i].nr].nod[3]].nz+node[e_enqire[e_enqire[i].nr].nod[5]].nz )  + 0.5*(
5272           node[e_enqire[e_enqire[i].nr].nod[ 8]].nz+node[e_enqire[e_enqire[i].nr].nod[ 9]].nz   +
5273           node[e_enqire[e_enqire[i].nr].nod[14]].nz+node[e_enqire[e_enqire[i].nr].nod[11]].nz) ;
5274 
5275           /* create  new node in center of area4 */
5276           node[e_enqire[e_enqire[i].nr].nod[18  ]].nx = -0.25* (
5277           node[e_enqire[e_enqire[i].nr].nod[0]].nx+node[e_enqire[e_enqire[i].nr].nod[2]].nx    +
5278           node[e_enqire[e_enqire[i].nr].nod[1]].nx+node[e_enqire[e_enqire[i].nr].nod[0]].nx )  + 0.5*(
5279           node[e_enqire[e_enqire[i].nr].nod[ 8]].nx+node[e_enqire[e_enqire[i].nr].nod[ 7]].nx   +
5280           node[e_enqire[e_enqire[i].nr].nod[ 6]].nx+node[e_enqire[e_enqire[i].nr].nod[ 0]].nx) ;
5281 
5282           node[e_enqire[e_enqire[i].nr].nod[18  ]].ny = -0.25* (
5283           node[e_enqire[e_enqire[i].nr].nod[0]].ny+node[e_enqire[e_enqire[i].nr].nod[2]].ny    +
5284           node[e_enqire[e_enqire[i].nr].nod[1]].ny+node[e_enqire[e_enqire[i].nr].nod[0]].ny )  + 0.5*(
5285           node[e_enqire[e_enqire[i].nr].nod[ 8]].ny+node[e_enqire[e_enqire[i].nr].nod[ 7]].ny   +
5286           node[e_enqire[e_enqire[i].nr].nod[ 6]].ny+node[e_enqire[e_enqire[i].nr].nod[ 0]].ny) ;
5287 
5288           node[e_enqire[e_enqire[i].nr].nod[18  ]].nz = -0.25* (
5289           node[e_enqire[e_enqire[i].nr].nod[0]].nz+node[e_enqire[e_enqire[i].nr].nod[2]].nz    +
5290           node[e_enqire[e_enqire[i].nr].nod[1]].nz+node[e_enqire[e_enqire[i].nr].nod[0]].nz )  + 0.5*(
5291           node[e_enqire[e_enqire[i].nr].nod[ 8]].nz+node[e_enqire[e_enqire[i].nr].nod[ 7]].nz   +
5292           node[e_enqire[e_enqire[i].nr].nod[ 6]].nz+node[e_enqire[e_enqire[i].nr].nod[ 0]].nz) ;
5293 
5294           /* create  new node in center of area5 */
5295           node[e_enqire[e_enqire[i].nr].nod[19  ]].nx = -0.25* (
5296           node[e_enqire[e_enqire[i].nr].nod[3]].nx+node[e_enqire[e_enqire[i].nr].nod[4]].nx    +
5297           node[e_enqire[e_enqire[i].nr].nod[5]].nx+node[e_enqire[e_enqire[i].nr].nod[3]].nx )  + 0.5*(
5298           node[e_enqire[e_enqire[i].nr].nod[12]].nx+node[e_enqire[e_enqire[i].nr].nod[13]].nx   +
5299           node[e_enqire[e_enqire[i].nr].nod[14]].nx+node[e_enqire[e_enqire[i].nr].nod[ 3]].nx) ;
5300 
5301           node[e_enqire[e_enqire[i].nr].nod[19  ]].ny = -0.25* (
5302           node[e_enqire[e_enqire[i].nr].nod[3]].ny+node[e_enqire[e_enqire[i].nr].nod[4]].ny    +
5303           node[e_enqire[e_enqire[i].nr].nod[5]].ny+node[e_enqire[e_enqire[i].nr].nod[3]].ny )  + 0.5*(
5304           node[e_enqire[e_enqire[i].nr].nod[12]].ny+node[e_enqire[e_enqire[i].nr].nod[13]].ny   +
5305           node[e_enqire[e_enqire[i].nr].nod[14]].ny+node[e_enqire[e_enqire[i].nr].nod[ 3]].ny) ;
5306 
5307           node[e_enqire[e_enqire[i].nr].nod[19  ]].nz = -0.25* (
5308           node[e_enqire[e_enqire[i].nr].nod[3]].nz+node[e_enqire[e_enqire[i].nr].nod[4]].nz    +
5309           node[e_enqire[e_enqire[i].nr].nod[5]].nz+node[e_enqire[e_enqire[i].nr].nod[3]].nz )  + 0.5*(
5310           node[e_enqire[e_enqire[i].nr].nod[12]].nz+node[e_enqire[e_enqire[i].nr].nod[13]].nz   +
5311           node[e_enqire[e_enqire[i].nr].nod[14]].nz+node[e_enqire[e_enqire[i].nr].nod[ 3]].nz) ;
5312         }
5313         if(e_enqire[e_enqire[i].nr].type==10)
5314         {
5315           node[e_enqire[e_enqire[i].nr].nod[ 8]].nx = -0.25* (
5316           node[e_enqire[e_enqire[i].nr].nod[0]].nx+node[e_enqire[e_enqire[i].nr].nod[1]].nx  +
5317           node[e_enqire[e_enqire[i].nr].nod[3]].nx+node[e_enqire[e_enqire[i].nr].nod[2]].nx )+ 0.5*(
5318           node[e_enqire[e_enqire[i].nr].nod[4]].nx+node[e_enqire[e_enqire[i].nr].nod[6]].nx  +
5319           node[e_enqire[e_enqire[i].nr].nod[7]].nx+node[e_enqire[e_enqire[i].nr].nod[5]].nx) ;
5320 
5321           node[e_enqire[e_enqire[i].nr].nod[ 8]].ny = -0.25* (
5322           node[e_enqire[e_enqire[i].nr].nod[0]].ny+node[e_enqire[e_enqire[i].nr].nod[1]].ny  +
5323           node[e_enqire[e_enqire[i].nr].nod[3]].ny+node[e_enqire[e_enqire[i].nr].nod[2]].ny )+ 0.5*(
5324           node[e_enqire[e_enqire[i].nr].nod[4]].ny+node[e_enqire[e_enqire[i].nr].nod[6]].ny  +
5325           node[e_enqire[e_enqire[i].nr].nod[7]].ny+node[e_enqire[e_enqire[i].nr].nod[5]].ny) ;
5326 
5327           node[e_enqire[e_enqire[i].nr].nod[ 8]].nz = -0.25* (
5328           node[e_enqire[e_enqire[i].nr].nod[0]].nz+node[e_enqire[e_enqire[i].nr].nod[1]].nz  +
5329           node[e_enqire[e_enqire[i].nr].nod[3]].nz+node[e_enqire[e_enqire[i].nr].nod[2]].nz )+ 0.5*(
5330           node[e_enqire[e_enqire[i].nr].nod[4]].nz+node[e_enqire[e_enqire[i].nr].nod[6]].nz  +
5331           node[e_enqire[e_enqire[i].nr].nod[7]].nz+node[e_enqire[e_enqire[i].nr].nod[5]].nz) ;
5332 	}
5333       }
5334 }
5335 
5336 
5337 
5338 /* generates or fixes nodes in the mid of the element face for drawing purposes only */
5339 /* flag true (1) if new nodes must be allocated */
adjustDrawNodes(int flag)5340 void adjustDrawNodes(int flag)
5341 {
5342   int i,j,n, lc;
5343 
5344   if(flag)
5345   {
5346     /* nodes of -qcut were placed behind the last address of the midside nodes, delete the elems */
5347     zap(specialset->etmp);
5348 
5349     /* new number of original nodes */
5350     anz->orignmax = anz->nmax;
5351     anz->orign = anz->n;
5352 
5353     /* allocate midside nodes */
5354     n=0;
5355     for ( i=0; i<anz->e; i++ )
5356     {
5357       switch(e_enqire[e_enqire[i].nr].type)
5358       {
5359         case 4:
5360 	  n+=6;
5361           for(j=0; j<6; j++) e_enqire[e_enqire[i].nr].nod[j+20]=++anz->nmax;
5362 	break;
5363         case 5:
5364 	  n+=5;
5365           for(j=0; j<5; j++) e_enqire[e_enqire[i].nr].nod[j+15]=++anz->nmax;
5366 	break;
5367         case 10:
5368 	  n++;
5369           e_enqire[e_enqire[i].nr].nod[8]=++anz->nmax;
5370 	break;
5371       }
5372     }
5373     if ((node = (Nodes *)realloc( (Nodes *)node, (anz->nmax+1)*sizeof(Nodes)) ) == NULL )
5374     { errMsg("ERROR: realloc failure in nod, node:%d not installed\n", anz->nmax); return; }
5375 
5376     anz->nmax = anz->orignmax;
5377     for (i=0; i<n; i++)
5378     {
5379       node[anz->n].nr=++anz->nmax;
5380       node[node[anz->n].nr].indx=anz->n;
5381       node[node[anz->n].nr].pflag=1;
5382       anz->n++;
5383     }
5384 
5385     /* extend the lc by the new node */
5386     for (lc=0; lc<anz->l; lc++)
5387     {
5388       if (lcase[lc].loaded)
5389       {
5390         for(i=0; i<lcase[lc].ncomps; i++)
5391         {
5392           if ( (lcase[lc].dat[i] = (float *)realloc(lcase[lc].dat[i], (anz->nmax+1) * sizeof(float))) == NULL )
5393             printf("\n\n ERROR: realloc failure nod\n\n" );
5394 	  for(j=1; j<=n; j++ ) lcase[lc].dat[i][anz->orignmax+j]=0.;
5395         }
5396       }
5397     }
5398   }
5399   posMidsideNodes(node);
5400   //updateDispLists();
5401 }
5402 
5403 
5404 
write2stack(int n,char ** parameter)5405 int write2stack(int n, char **parameter)
5406 {
5407   int i;
5408   if(!valuestackFlag) return(-1);
5409 
5410   if ((valuestack = (char **)realloc( (char **)valuestack, (valuestack_ptr+n)*sizeof(char *)) ) == NULL )
5411   { printf("\n\nERROR: realloc failure, valuestack\n\n"); return(-1); }
5412   for(i=0; i<n; i++)
5413   {
5414     if ((valuestack[valuestack_ptr] = (char *)malloc( MAX_LINE_LENGTH*sizeof(char)) ) == NULL )
5415     { printf("\n\nERROR: realloc failure, valuestack\n\n"); return(-1); }
5416     sprintf(valuestack[valuestack_ptr++],"%s", parameter[n-1-i] );
5417   }
5418   printf(" %d values in inverse order written to stack\n", n);
5419   return(n);
5420 }
5421 
5422 
5423 
enquireEntities(char * string)5424 int enquireEntities(char *string)
5425 {
5426   int i,j,k,l, m,n, args;
5427   static int enq_nr=0;
5428   int ico[3], setPos=-1, setNr=-1, trgtSet=-1, nval=0, valFlag=0;
5429   int csys[3];
5430   char dat[9][MAX_LINE_LENGTH], filenam[MAX_LINE_LENGTH], mode='i';
5431   double vco[3], tol=0., val=0, nx=0,ny,nz, dx, dy, dz, nr=0, nf, dr, df, rval=0, value=0.;
5432   double pr[3], pc[3];
5433   Rsort *rsort=NULL;
5434   FILE *handle;
5435 
5436   // enq set trgt_set rec|cx|cy|cz|set 3vals|setname ('-' == all possible vals, r+fi+z) tol i|a|h|l [value] (9 args)
5437   args=sscanf(string,"%s %s %s %s %s %s %s %s %s", dat[0], dat[1], dat[2], dat[3], dat[4], dat[5], dat[6], dat[7], dat[8]);
5438 
5439   if(anz->l)
5440   {
5441     /* check if the data of the specified lcase (Dataset) are already available */
5442     if (!lcase[cur_lc].loaded)
5443     {
5444       if( pre_readfrdblock(copiedNodeSets , cur_lc, anz, node, lcase )==-1)
5445       {
5446         printf("ERROR in nodalDataset: Could not read data for Dataset:%d\n", cur_lc+1);
5447         return(0);
5448       }
5449       calcDatasets( cur_lc, anz, node, lcase );
5450       recompileEntitiesInMenu(cur_lc);
5451     }
5452   }
5453 
5454   setNr=getSetNr(dat[0]);
5455   if(setNr<0)
5456   {
5457     printf("ERROR: set:%s does not exist\n", dat[0]);
5458     sprintf(parameter[0], "ERROR: set %s does not exist", dat[0]);
5459     write2stack(1, parameter);
5460     return(0);
5461   }
5462 
5463   trgtSet=getSetNr(dat[1]);
5464   if(trgtSet<0)
5465   {
5466     trgtSet=pre_seta(dat[1], "i", 0);
5467   }
5468 
5469   /* get the coordinates */
5470 
5471   /* based on a node or point in a set */
5472   if(dat[2][0]=='s')
5473   {
5474     setPos=getSetNr(dat[3]);
5475     if(setPos<0)
5476     {
5477       printf(" ERROR: Specified set:%s does not exist\n", dat[3]);
5478       return(0);
5479     }
5480     if((!set[setPos].anz_n)&&(!set[setPos].anz_p))
5481     {
5482       printf(" ERROR: Specified set:%s contains no node or point\n", dat[3]);
5483       return(0);
5484     }
5485     tol=atof(dat[4]);
5486     if(args==7) { value=atof(dat[6]); valFlag=1; }
5487     if(args>=6) mode=dat[5][0];
5488     else mode='h';
5489   }
5490   /* based on coordinates */
5491   else
5492   {
5493     for(i=0; i<3; i++)   if(dat[3+i][0]!='_') { vco[i]=atof(dat[3+i]); ico[i]=1; }  else { ico[i]=0; }
5494 
5495     tol=atof(dat[6]);
5496     if(args==9) { value=atof(dat[8]); valFlag=1; }
5497     if(args>=8) mode=dat[7][0];
5498     else
5499     {
5500       for(i=0; i<3; i++) if(!ico[i]) mode='a';
5501     }
5502   }
5503   printf("mode:%c tol:%f\n", mode, tol);
5504 
5505   descalNodes ( anz->n, node, scale );
5506   descalPoints( anzGeo->p, point, scale);
5507 
5508 
5509   if(set[setNr].anz_n)
5510   {
5511     /* calculate dr of all nodes and sort the indexes according to distance**2 (rsort[i].r) */
5512     if ( (rsort = (Rsort *)malloc( (set[setNr].anz_n+1) * sizeof(Rsort))) == NULL )
5513       printf("ERROR: realloc failed: Rsort\n\n" );
5514 
5515     for(i=0; i<set[setNr].anz_n; i++)
5516     {
5517       n=set[setNr].node[i];
5518       if(dat[2][0]=='s')
5519       {
5520         if(set[setPos].anz_n)
5521         {
5522           dx=node[set[setPos].node[0]].nx-node[n].nx;
5523           dy=node[set[setPos].node[0]].ny-node[n].ny;
5524           dz=node[set[setPos].node[0]].nz-node[n].nz;
5525 	}
5526         else if(set[setPos].anz_p)
5527         {
5528           dx=point[set[setPos].pnt[0]].px-node[n].nx;
5529           dy=point[set[setPos].pnt[0]].py-node[n].ny;
5530           dz=point[set[setPos].pnt[0]].pz-node[n].nz;
5531 	}
5532         else return(0);
5533         rsort[i].r=dx*dx+dy*dy+dz*dz;
5534       }
5535       else if(dat[2][0]=='r')
5536       {
5537         if(ico[0]) dx=vco[0]-node[n].nx; else dx=0.;
5538         if(ico[1]) dy=vco[1]-node[n].ny; else dy=0.;
5539         if(ico[2]) dz=vco[2]-node[n].nz; else dz=0.;
5540         rsort[i].r=dx*dx+dy*dy+dz*dz;
5541       }
5542       else if((dat[2][0]=='c')&&(dat[2][2]=='l'))
5543       {
5544         printf(" WARNING: The 'cyl x|y|z <r> <z>' option was replaced by 'cx|cy|cz <r> <fi> <z>' and will be deleted in future. Please consult the manual\n");
5545         switch(dat[3][0])
5546         {
5547           case 'x':
5548           {
5549             nr=sqrt(node[n].nz*node[n].nz+node[n].ny*node[n].ny);
5550             nx=node[n].nx;
5551           }
5552           break;
5553           case 'y':
5554           {
5555             nr=sqrt(node[n].nz*node[n].nz+node[n].nx*node[n].nx);
5556             nx=node[n].ny;
5557           }
5558           break;
5559           case 'z':
5560           {
5561             nr=sqrt(node[n].ny*node[n].ny+node[n].nx*node[n].nx);
5562             nx=node[n].nz;
5563           }
5564           break;
5565         }
5566         if(ico[1]) dr=vco[1]-nr; else dr=0.;
5567         if(ico[2]) dx=vco[2]-nx; else dx=0.;
5568         rsort[i].r=dr*dr+dx*dx;
5569 	//printf("n:%d dr:%f dx:%f\n", n,dr, dx);
5570       }
5571       else if(dat[2][0]=='c')
5572       {
5573         if( dat[2][1]=='x') j=0;
5574         else if( dat[2][1]=='y') j=1;
5575         else if( dat[2][1]=='z') j=2;
5576         else {  errMsg("\n\n ERROR: axis unknown\n"); return(0); };
5577         pr[0]=node[n].nx;
5578         pr[1]=node[n].ny;
5579         pr[2]=node[n].nz;
5580         if( v_rec2cyl( pr, j, csys, pc) ==-3) { rsort[i].r=MAX_FLOAT; continue; }
5581         nr=pc[0];
5582         nf=pc[1];
5583         nx=pc[2];
5584 
5585         if(ico[0]) dr=vco[0]-nr; else dr=0.;
5586         //if(ico[1]) df=vco[1]-nf; else df=0.;
5587         // user input was in degree:
5588         if(ico[1]) df=vco[1]*0.0174532925194-nf; else df=0.;
5589         if(ico[2]) dx=vco[2]-nx; else dx=0.;
5590         /* df is in rad but needs mm */
5591         df*=nr;
5592         rsort[i].r=dr*dr+df*df+dx*dx;
5593 	//printf("n:%d dr:%f df:%f[rad] dx:%f d**2:%f\n", n,dr,df,dx, rsort[i].r);
5594       }
5595       else
5596       {
5597         printf("parameter not recognized:%s\n", dat[2]);
5598         return(0);
5599       }
5600       rsort[i].i=n;
5601     }
5602     qsort( rsort, set[setNr].anz_n, sizeof(Rsort), (void *)compareRsort );
5603 
5604     switch(mode)
5605     {
5606       case 'i':
5607       {
5608         if(rsort[0].r<=tol*tol)
5609         {
5610 	  //printf("n:%d r:%f\n",  rsort[0].i, rsort[0].r);
5611           sprintf(parameter[0],"%d", rsort[0].i);
5612           sprintf(parameter[1],"%f", rsort[0].r);
5613           write2stack(2, parameter);
5614           seta(trgtSet, "n", rsort[0].i);
5615 	}
5616       }
5617       break;
5618       case 'a':
5619       {
5620         for (i=0; i<set[setNr].anz_n; i++)
5621         {
5622           if(rsort[i].r>tol*tol) break;
5623 	  //printf("n:%d r:%f\n",  rsort[i].i, rsort[i].r);
5624           sprintf(parameter[0],"%d", rsort[i].i);
5625           sprintf(parameter[1],"%f", rsort[i].r);
5626           write2stack(2, parameter);
5627           seta(trgtSet, "n", rsort[i].i);
5628         }
5629       }
5630       break;
5631       case 'h':
5632       {
5633         if(valFlag)
5634 	{
5635           /* search values above value in range */
5636           for (i=0; i<set[setNr].anz_n; i++)
5637           {
5638             if(rsort[i].r>tol*tol) break;
5639             if(lcase[cur_lc].dat[cur_entity][rsort[i].i]>=value)
5640             {
5641               val=lcase[cur_lc].dat[cur_entity][rsort[i].i]; nval=rsort[i].i; rval=rsort[i].r;
5642               seta(trgtSet, "n", nval);
5643               printf(" node:%d value:%lf dist:%lf\n", nval, val, sqrt(rval));
5644               sprintf(parameter[0],"%d", nval);
5645               sprintf(parameter[1],"%f", val);
5646               sprintf(parameter[2],"%f", sqrt(rval));
5647               write2stack(3, parameter);
5648             }
5649           }
5650 	}
5651         else
5652 	{
5653           /* search the highest value in range */
5654           val=-MAX_FLOAT;
5655           for (i=0; i<set[setNr].anz_n; i++)
5656           {
5657             if(rsort[i].r>tol*tol) break;
5658             if(val<lcase[cur_lc].dat[cur_entity][rsort[i].i]) { val=lcase[cur_lc].dat[cur_entity][rsort[i].i]; nval=rsort[i].i; rval=rsort[i].r; }
5659           }
5660           if(nval)
5661           {
5662             seta(trgtSet, "n", nval);
5663             printf(" node:%d value:%lf dist:%lf\n\n", nval, val, sqrt(rval));
5664             sprintf(parameter[0],"%d", nval);
5665             sprintf(parameter[1],"%f", val);
5666             sprintf(parameter[2],"%f", sqrt(rval));
5667             write2stack(3, parameter);
5668           }
5669           else printf("\n found no node in range\n\n");
5670 	}
5671       }
5672       break;
5673       case 'l':
5674       {
5675         if(valFlag)
5676 	{
5677           /* search values below value in range */
5678           for (i=0; i<set[setNr].anz_n; i++)
5679           {
5680             if(rsort[i].r>tol*tol) break;
5681             if(lcase[cur_lc].dat[cur_entity][rsort[i].i]<=value)
5682             {
5683               val=lcase[cur_lc].dat[cur_entity][rsort[i].i]; nval=rsort[i].i; rval=rsort[i].r;
5684               seta(trgtSet, "n", nval);
5685               printf(" node:%d value:%lf dist:%lf\n", nval, val, sqrt(rval));
5686               sprintf(parameter[0],"%d", nval);
5687               sprintf(parameter[1],"%f", val);
5688               sprintf(parameter[2],"%f", sqrt(rval));
5689               write2stack(3, parameter);
5690             }
5691           }
5692 	}
5693         else
5694 	{
5695           val=MAX_FLOAT;
5696           /* search the lowest value in range */
5697           for (i=0; i<set[setNr].anz_n; i++)
5698           {
5699             if(rsort[i].r>tol*tol) break;
5700             if(val>lcase[cur_lc].dat[cur_entity][rsort[i].i]) { val=lcase[cur_lc].dat[cur_entity][rsort[i].i]; nval=rsort[i].i; rval=rsort[i].r; }
5701           }
5702           if(nval)
5703 	  {
5704             seta(trgtSet, "n", nval);
5705             printf(" node:%d value:%lf dist:%lf\n\n", nval, val, sqrt(rval));
5706             sprintf(parameter[0],"%d", nval);
5707             sprintf(parameter[1],"%f", val);
5708             sprintf(parameter[2],"%f", sqrt(rval));
5709             write2stack(3, parameter);
5710 	  }
5711           else printf("\n found no node in range\n\n");
5712 	}
5713       }
5714       break;
5715     }
5716     free(rsort);
5717 
5718     if((mode=='l')||(mode=='h'))
5719     {
5720       enq_nr++;
5721       sprintf(filenam,"enq_lc%d_e%d_%d.out", cur_lc+1, cur_entity+1, enq_nr);
5722       handle = fopen (filenam, "w+b");
5723       if (handle==NULL) { printf ("\nThe output file \"%s\" could not be opened.\n\n", filenam ); }
5724       else
5725       {
5726         printf (" result is written to \"%s\"\n\n", filenam);
5727         fprintf(handle, " node: %d val: %f dist: %f\n", nval, val, sqrt(rval));
5728         fclose(handle);
5729       }
5730     }
5731   }
5732 
5733   if(set[setNr].anz_p)
5734   {
5735     /* calculate dr of all points and sort the indexes according to distance**2 (rsort[i].r) */
5736     if ( (rsort = (Rsort *)malloc( (set[setNr].anz_p+1) * sizeof(Rsort))) == NULL )
5737       printf("ERROR: realloc failed: Rsort\n\n" );
5738 
5739     for(i=0; i<set[setNr].anz_p; i++)
5740     {
5741       n=set[setNr].pnt[i];
5742       if(dat[2][0]=='s')
5743       {
5744         if(set[setPos].anz_n)
5745         {
5746           dx=node[set[setPos].node[0]].nx-node[n].nx;
5747           dy=node[set[setPos].node[0]].ny-node[n].ny;
5748           dz=node[set[setPos].node[0]].nz-node[n].nz;
5749 	}
5750         else if(set[setPos].anz_p)
5751         {
5752           dx=point[set[setPos].pnt[0]].px-node[n].nx;
5753           dy=point[set[setPos].pnt[0]].py-node[n].ny;
5754           dz=point[set[setPos].pnt[0]].pz-node[n].nz;
5755 	}
5756         else return(0);
5757         rsort[i].r=dx*dx+dy*dy+dz*dz;
5758       }
5759       else if(dat[2][0]=='r')
5760       {
5761         if(ico[0]) dx=vco[0]-point[n].px; else dx=0.;
5762         if(ico[1]) dy=vco[1]-point[n].py; else dy=0.;
5763         if(ico[2]) dz=vco[2]-point[n].pz; else dz=0.;
5764         rsort[i].r=dx*dx+dy*dy+dz*dz;
5765       }
5766       else if(dat[2][0]=='c')
5767       {
5768         switch(dat[3][0])
5769         {
5770           case 'x':
5771           {
5772             nr=sqrt(point[n].pz*point[n].pz+point[n].py*point[n].py);
5773             nx=point[n].px;
5774           }
5775           break;
5776           case 'y':
5777           {
5778             nr=sqrt(point[n].pz*point[n].pz+point[n].px*point[n].px);
5779             nx=point[n].py;
5780           }
5781           break;
5782           case 'z':
5783           {
5784             nr=sqrt(point[n].py*point[n].py+point[n].px*point[n].px);
5785             nx=point[n].pz;
5786           }
5787           break;
5788         }
5789         if(ico[1]) dr=vco[1]-nr; else dr=0.;
5790         if(ico[2]) dx=vco[2]-nx; else dx=0.;
5791         rsort[i].r=dr*dr+dx*dx;
5792       }
5793       else
5794       {
5795         printf("parameter not recognized:%s\n", dat[2]);
5796         return(0);
5797       }
5798       rsort[i].i=n;
5799     }
5800     qsort( rsort, set[setNr].anz_p, sizeof(Rsort), (void *)compareRsort );
5801 
5802     switch(mode)
5803     {
5804       case 'i':
5805       {
5806         if(rsort[0].r<=tol*tol)
5807         {
5808 	  printf("p:%s r:%f\n",  point[rsort[0].i].name, rsort[0].r);
5809           seta(trgtSet, "p", rsort[0].i);
5810           sprintf(parameter[0],"%s", point[rsort[0].i].name);
5811           sprintf(parameter[1],"%f", rsort[0].r);
5812           write2stack(2, parameter);
5813 	}
5814       }
5815       break;
5816       case 'a':
5817       {
5818         for (i=0; i<set[setNr].anz_p; i++)
5819         {
5820           if(rsort[i].r>tol*tol) break;
5821 	  printf("p:%s r:%f\n",  point[rsort[i].i].name, rsort[i].r);
5822           seta(trgtSet, "p", rsort[i].i);
5823           sprintf(parameter[0],"%s", point[rsort[i].i].name);
5824           sprintf(parameter[1],"%f", rsort[i].r);
5825           write2stack(2, parameter);
5826         }
5827       }
5828       break;
5829     }
5830     free(rsort);
5831   }
5832 
5833 
5834   if(set[setNr].anz_l)
5835   {
5836     /* calculate dr of all points and sort the indexes according to distance**2 (rsort[i].r) */
5837     if ( (rsort = (Rsort *)malloc( (set[setNr].anz_l+1)*ddiv  * sizeof(Rsort))) == NULL )
5838       printf("ERROR: realloc failed: Rsort\n\n" );
5839 
5840     j=0;
5841     for(i=0; i<set[setNr].anz_l; i++)
5842     {
5843       l=set[setNr].line[i];
5844       for (n=0; n<line[l].nip; n+=3)
5845       {
5846         if(dat[2][0]=='r')
5847         {
5848           if(ico[0]) dx=vco[0]-((line[l].ip[n]* scale->w)+scale->x); else dx=0.;
5849           if(ico[1]) dy=vco[1]-((line[l].ip[n+1]* scale->w)+scale->y); else dy=0.;
5850           if(ico[2]) dz=vco[2]-((line[l].ip[n+2]* scale->w)+scale->z); else dz=0.;
5851           rsort[j].r=dx*dx+dy*dy+dz*dz;
5852 	  //printf("r:%lf %lf %lf %lf\n", rsort[j].r, dx,dy,dz);
5853         }
5854         else if(dat[2][0]=='c')
5855         {
5856           nx=(line[l].ip[n]* scale->w)+scale->x;
5857           ny=(line[l].ip[n+1]* scale->w)+scale->y;
5858           nz=(line[l].ip[n+2]* scale->w)+scale->z;
5859 	  //printf("pnt ! %f %f %f\n",nx,ny,nz);
5860           switch(dat[3][0])
5861           {
5862             case 'x':
5863             {
5864               nr=sqrt(ny*ny+nz*nz);
5865               //nx=nx;
5866             }
5867             break;
5868             case 'y':
5869             {
5870               nr=sqrt(nx*nx+nz*nz);
5871               nx=ny;
5872             }
5873             break;
5874             case 'z':
5875             {
5876               nr=sqrt(ny*ny+nx*nx);
5877               nx=nz;
5878             }
5879             break;
5880           }
5881           if(ico[1]) dr=vco[1]-nr; else dr=0.;
5882           if(ico[2]) dx=vco[2]-nx; else dx=0.;
5883           rsort[j].r=dr*dr+dx*dx;
5884         }
5885         else
5886         {
5887           printf("parameter not recognized:%s\n", dat[2]);
5888           return(0);
5889         }
5890         rsort[j].i=l;
5891 
5892         j++;
5893         if(j>=(set[setNr].anz_l+1)*ddiv  )
5894 	{
5895           if ( (rsort = (Rsort *)realloc((Rsort *)rsort,  (j+1)  * sizeof(Rsort))) == NULL )
5896             printf("ERROR: realloc failed: Rsort\n\n" );
5897 	}
5898       }
5899     }
5900     if(j==0) goto nolines;
5901 
5902     qsort( rsort, j, sizeof(Rsort), (void *)compareRsort );
5903 
5904     switch(mode)
5905     {
5906       case 'i':
5907       {
5908         if(rsort[0].r<=tol*tol)
5909 	{
5910           seta(trgtSet, "l", rsort[0].i);
5911           sprintf(parameter[0],"%s", line[rsort[0].i].name);
5912           sprintf(parameter[1],"%f", rsort[0].r);
5913           write2stack(2, parameter);
5914 	}
5915       }
5916       break;
5917       case 'a':
5918       {
5919         for (i=0; i<j; i++)
5920         {
5921           if(rsort[i].r>tol*tol) break;
5922           seta(trgtSet, "l", rsort[i].i);
5923           sprintf(parameter[0],"%s", line[rsort[i].i].name);
5924           sprintf(parameter[1],"%f", rsort[i].r);
5925           write2stack(2, parameter);
5926         }
5927       }
5928       break;
5929     }
5930   nolines:;
5931     free(rsort);
5932   }
5933 
5934 
5935   if(set[setNr].anz_s)
5936   {
5937     /* calculate dr of all points and sort the indexes according to distance**2 (rsort[i].r) */
5938     if ( (rsort = (Rsort *)malloc( (set[setNr].anz_s+1)*ddiv*ddiv  * sizeof(Rsort))) == NULL )
5939       printf("ERROR: realloc failed: Rsort\n\n" );
5940 
5941     j=0;
5942     for(i=0; i<set[setNr].anz_s; i++)
5943     {
5944       l=set[setNr].surf[i];
5945       n=0;
5946       while((surf[l].npgn-n))
5947       {
5948         n++; /* jump over the polygon token (ie.GL_POLYGON_TOKEN) */
5949         m=surf[l].pgn[n++];
5950         n+=3; /* jump over the normal-vector */
5951         for(k=0; k<m; k++)
5952         {
5953           //printf("%d %s %lf %lf %lf\n", k,surf[l].name, surf[l].pgn[n],surf[l].pgn[n+1],surf[l].pgn[n+2]);
5954           if(dat[2][0]=='r')
5955           {
5956             if(ico[0]) dx=vco[0]-((surf[l].pgn[n]* scale->w)+scale->x); else dx=0.;
5957             if(ico[1]) dy=vco[1]-((surf[l].pgn[n+1]* scale->w)+scale->y); else dy=0.;
5958             if(ico[2]) dz=vco[2]-((surf[l].pgn[n+2]* scale->w)+scale->z); else dz=0.;
5959             rsort[j].r=dx*dx+dy*dy+dz*dz;
5960   	    //printf("r:%lf %lf %lf %lf\n", rsort[j].r, dx,dy,dz);
5961           }
5962           else if(dat[2][0]=='c')
5963           {
5964             nx=(surf[l].pgn[n]* scale->w)+scale->x;
5965             ny=(surf[l].pgn[n+1]* scale->w)+scale->y;
5966             nz=(surf[l].pgn[n+2]* scale->w)+scale->z;
5967   	    //printf("pnt ! %f %f %f\n",nx,ny,nz);
5968             switch(dat[3][0])
5969             {
5970               case 'x':
5971               {
5972                 nr=sqrt(ny*ny+nz*nz);
5973                 //nx=nx;
5974               }
5975               break;
5976               case 'y':
5977               {
5978                 nr=sqrt(nx*nx+nz*nz);
5979                 nx=ny;
5980               }
5981               break;
5982               case 'z':
5983               {
5984                 nr=sqrt(ny*ny+nx*nx);
5985                 nx=nz;
5986               }
5987               break;
5988             }
5989             if(ico[1]) dr=vco[1]-nr; else dr=0.;
5990             if(ico[2]) dx=vco[2]-nx; else dx=0.;
5991             rsort[j].r=dr*dr+dx*dx;
5992           }
5993           else
5994           {
5995             printf("parameter not recognized:%s\n", dat[2]);
5996             return(0);
5997           }
5998           rsort[j].i=l;
5999 
6000           j++;
6001           if(j>=(set[setNr].anz_s+1)*ddiv*ddiv  )
6002   	  {
6003             if ( (rsort = (Rsort *)realloc((Rsort *)rsort,  (j+1)  * sizeof(Rsort))) == NULL )
6004               printf("ERROR: realloc failed: Rsort\n\n" );
6005           }
6006 
6007           n+=3;
6008         }
6009       }
6010     }
6011     if(j==0) goto nosurfs;
6012 
6013     qsort( rsort, j, sizeof(Rsort), (void *)compareRsort );
6014 
6015     switch(mode)
6016     {
6017       case 'i':
6018       {
6019         if(rsort[0].r<=tol*tol)
6020 	{
6021           seta(trgtSet, "s", rsort[0].i);
6022           sprintf(parameter[0],"%s", surf[rsort[0].i].name);
6023           sprintf(parameter[1],"%f", rsort[0].r);
6024           write2stack(2, parameter);
6025 	}
6026       }
6027       break;
6028       case 'a':
6029       {
6030         for (i=0; i<j; i++)
6031         {
6032           if(rsort[i].r>tol*tol) break;
6033           seta(trgtSet, "s", rsort[i].i);
6034           sprintf(parameter[0],"%s", surf[rsort[i].i].name);
6035           sprintf(parameter[1],"%f", rsort[i].r);
6036           write2stack(2, parameter);
6037         }
6038       }
6039       break;
6040     }
6041   nosurfs:;
6042     free(rsort);
6043   }
6044 
6045 
6046   scalNodes ( anz->n, node, scale );
6047   scalPoints( anzGeo->p, point, scale);
6048 
6049   return(1);
6050 }
6051