1
/*
2
 *=============================================================================
3
 *  Main program for DATCOM-MODELER
4
 *
5
 *  Copyright (C) 2009  Anders Gidenstam (anders(at)gidenstam.org)
6
 *  Copyright (C) 2009  Ronald Jensen    (ron(at)jentronics.com)
7
 *  http://www.gidenstam.org
8
 *  http://www.jentronics.com
9
 *
10
 *  This program is free software; you can redistribute it and/or modify
11
 *  it under the terms of the GNU General Public License as published by
12
 *  the Free Software Foundation; either version 3 of the License, or
13
 *  (at your option) any later version.
14
 *
15
 *  This program is distributed in the hope that it will be useful,
16
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 *  GNU General Public License for more details.
19
 *
20
 *  You should have received a copy of the GNU General Public License
21
 *  along with this program; if not, write to the Free Software
22
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23
 *=============================================================================
24
 */
25
26
#include <unistd.h>
27
#include <stdio.h>
28
#include <stdlib.h>
29
#include "datcom-parser.h"
30
#include "modeler_proto.h"
31
32
int verbose=1; /* External to all other modules */
33
/* Verbose Levels:
34
 *
35
 * 0. Quiet: No output on stderr
36
 * 1. Default: Print errors
37
 * 2. Chatty: Print interesting trivia, warnings
38
 * 3. Verbose: Print everything
39
 *
40
 */
41
int dofoils(DATCOM_AIRFOIL *datcomfoil, struct AIRFOIL *foil, char * defaultfoil);
42
43
44
void PrintAC(struct AIRCRAFT *ac)
45
{
46
    return;
47
}
48
49
void Usage(char *name)
50
{
51
    fprintf(stderr, "Usage: %s [-n b|e|f|h|v|w] [-v] [-q] [-Q] [-o ac-file] datcom-file\n", name);
52
}
53
54
int main(int argc, char *argv[])
55
{
56
    AIRCRAFT ac;
57
58
/* We read these on the command line */
59
    int wing=1;
60
    int htail=1;
61
    int vtail=1;
62
    int vfin=1;
63
    int body=1;
64
    int engines=1;
65
    int opt;
66
    int quads=0;
67
    FILE *ofp = stdout;
68
    char *outputfile=NULL;
69
70
71
/* internal variables */
72
    int objects = 0;
73
    int propellers = 0;
74
    int shiny = 0;
75
    struct AIRFOIL wingfoil;
76
    struct AIRFOIL htailfoil;
77
    struct AIRFOIL vtailfoil;
78
    struct AIRFOIL vfinfoil;
79
80
    while ((opt = getopt(argc, argv, "n:o:qvQ")) != -1)
81
    {
82
        switch (opt)
83
        {
84
            case 'n':
85
                switch(optarg[0])
86
                {
87
                    case 'b':
88
                        body=0;
89
                        break;
90
                    case 'e':
91
                        engines = 0;
92
                        break;
93
                    case 'f':
94
                        vfin = 0;
95
                        break;
96
                    case 'h':
97
                        htail=0;
98
                        break;
99
                    case 'v':
100
                        vtail=0;
101
                        break;
102
                    case 'w':
103
                        wing=0;
104
                        break;
105
                    default:
106
                        Usage(argv[0]);
107
                        exit(EXIT_FAILURE);
108
                }
109
                break;
110
            case 'o':
111
                outputfile=optarg;
112
                break;
113
            case 'v':
114
                verbose++;
115
                break;
116
            case 'q':
117
                verbose = 0;
118
                break;
119
            case 'Q':
120
                quads=1;
121
                break;
122
            default: /* '?' */
123
                Usage(argv[0]);
124
                exit(EXIT_FAILURE);
125
        }
126
    }
127
128
//  fprintf(stderr, "verbose=%d; optind=%d\n", verbose, optind);
129
130
    if (optind >= argc) {
131
        Usage(argv[0]);
132
        exit(EXIT_FAILURE);
133
    }
134
135
    ReadDatcom(argv[optind], &ac);
136
137
/* ReadDatcom will exit() on error so don't create the output file until after it is called */
138
    if(outputfile)
139
    {
140
        fprintf(stderr, "Creating file: %s\n",  outputfile);
141
        if( ( ofp = fopen(outputfile, "w")) == NULL)
142
        {
143
            fprintf(stderr,"Unable to open %s for writing\n", outputfile);
144
            exit(EXIT_FAILURE);
145
        }
146
    }
147
148
    PrintAC(&ac);
149
150
/* Process wingfoil */
151
    if (wing) wing = ac.wing.CHRDR > 0. ? 1 : 0; // only render if root chord exists
152
    if (wing) wing = dofoils(&ac.wingfoil, &wingfoil, "NACA-W-4-2414");
153
    if (wing) objects += 2;
154
155
/* Process htailfoil */
156
    if (htail) htail = ac.htail.CHRDR > 0. ? 1 : 0; // only render if root chord exists
157
    if (htail) htail =  dofoils(&ac.htailfoil, &htailfoil, "NACA-H-4-0014");
158
    if (htail) objects += 2;
159
160
/* Process vtailfoil */
161
    if (vtail) vtail = ac.vtail.CHRDR > 0. ? 1 : 0; // only render if root chord exists
162
        if (vtail) vtail =  dofoils(&ac.vtailfoil, &vtailfoil, "NACA-V-4-0012");
163
    if (vtail) objects += 1;
164
165
//* Process vfinfoil */
166
    if (vfin) vfin = ac.vfin.CHRDR > 0. ? 1 : 0; // only render if root chord exists
167
        if (vfin) vfin =  dofoils(&ac.vfinfoil, &vfinfoil, "NACA-F-4-0012");
168
    if (vfin) objects += 1;
169
170
/* Process Body */
171
    if (body) body=ac.body.NX > 0. ? 1 : 0;
172
    if (body) objects +=1;
173
174
/* Process Engines */
175
    if (engines)
176
    {
177
        propellers=ac.propwr.NENGSP;
178
        objects += propellers;
179
    }
180
      if(ac.optins.ROUGFC == 0.0) shiny=65;
181
      else shiny = GetShine(ac.optins.ROUGFC);
182
183
      InitAC(ofp, objects, shiny);
184
      if (wing) WriteWing(ofp, &ac.wing, &wingfoil, "Wing", ac.synths.XW, ac.synths.ZW, quads);
185
      if (htail) WriteWing(ofp, &ac.htail, &htailfoil, "H-Tail", ac.synths.XH, ac.synths.ZH, quads);
186
      if (vtail) WriteFin(ofp, &ac.vtail, &vtailfoil, "V-Tail", ac.synths.XV, ac.synths.ZV, 1, quads);
187
      if (vfin) WriteFin(ofp, &ac.vfin, &vfinfoil, "V-Fin", ac.synths.XVF, ac.synths.ZVF, ac.synths.VERTUP, quads);
188
      if (body) WriteBody(ofp, &ac.body, &ac.synths, quads);
189
      if (propellers) WritePropellers(ofp, &ac.propwr);
190
    return(0);
191
}
192
193
int dofoils(DATCOM_AIRFOIL *datcomfoil, struct AIRFOIL *foil, char * defaultfoil)
194
{
195
        if (datcomfoil->NPTS && datcomfoil->YUPPER && datcomfoil->YLOWER)
196
                DatcomFoil(datcomfoil, foil);
197
        else if(datcomfoil->NACA_DESCR)
198
    {
199
              NacaFoil(datcomfoil->NACA_DESCR, foil, 20);
200
              foil->COUNT--;
201
    }
202
    else if(defaultfoil)
203
    {
204
        NacaFoil(defaultfoil, foil, 20);
205
        foil->COUNT--;
206
    }
207
        else return(0);
208
209
210
    return(1);
211
}